(C#)调⽤Webservice,提⽰远程服务器返回错误(500)内
部服务器错误
因为⼯作需要调⽤WebService接⼝,查了下资料,发现添加服务引⽤可以直接调⽤websevice
如果不添加服务引⽤⼜怎么做呢?于是⼜去查看怎么根据http协议调⽤webservice并做了个⽆参接⼝测试,如下:
测试代码如下:
1protected void Page_Load(object sender, EventArgs e)
2        {
3            Bitmap bmp = new Bitmap(System.Drawing.Image.FromFile("C:/Users/TYTD/Desktop/测试样本/ch_DJI_279.jpg"));
4            MemoryStream ms = new MemoryStream();
5            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
6            ms.Flush();
7//将⼆进制数据存到byte数组中
8byte[] bmpBytes1 = ms.ToArray();
9            bmp.Dispose();
10
11string bmpBytes = HttpUtility.UrlEncode("bmpBytes") + "=" + HttpServerUtility.UrlTokenEncode(bmpBytes1);
12
13string url = "192.168.0.28:9800/WebService1.asmx/Send_Image";
14string a = CallServiceByGet1(url, bmpBytes);
15
16        }
17public static string CallServiceByGet1(string strURL,string a)
18        {
19var result = string.Empty;
20//创建⼀个HTTP请求
21byte[] byt = Encoding.UTF8.GetBytes(a);
22            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
23            request.Method = "POST";
24            request.ContentType = "application/x-www-form-urlencoded";
25            request.ContentLength = byt.Length;
26
27            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
28            System.IO.Stream outputStream = request.GetRequestStream();
29            outputStream.Write(byt, 0, byt.Length);
30            outputStream.Close();
31
32            HttpWebResponse response;
33            Stream responseStream;
34            StreamReader reader;
35string srcString;
36try
37            {
38                response = (HttpWebResponse)request.GetResponse();//获取http请求的响应对象
39            }
调用webservice服务40catch (WebException ex)
41            {
42return ex.Message;
43            }
44            responseStream = response.GetResponseStream();
45            reader = new System.IO.StreamReader(responseStream, Encoding.GetEncoding("UTF-8"));
46            srcString = reader.ReadToEnd();
47            result = srcString;  //返回值赋值
48            reader.Close();
49
50return result;
51        }

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。