(C#)调用Webservice,提示远程服务器返回错误(500)内部服务器错误

 因为工作需要调用WebService接口,查了下资料,发现添加服务引用可以直接调用websevice

参考地址:https://www.cnblogs.com/peterpc/p/4628441.html

如果不添加服务引用又怎么做呢?于是又去查看怎么根据http协议调用webservice并做了个无参接口测试,如下:(C#)调用Webservice,提示远程服务器返回错误(500)内部服务器错误_第1张图片

但一做有参的接口调用就提示500错误(远程服务器返回错误(500)内部服务器错误),查了半天资料,大多数都说是ContentType = "application/x-www-form-urlencoded; charset=UTF-8"; 改成ContentType = "text/html";或者在<@Page..%>中设置 ValidateRequest="false" 即可(这里无需修改Content-type)。结果还是报一样的错误。最后再https://www.jb51.net/article/120015.htm中发现参数是要拼接一下的 (param = HttpUtility.UrlEncode("param1") + "=" + HttpUtility.UrlEncode(num1) + "&" + HttpUtility.UrlEncode("param2") + "=" + HttpUtility.UrlEncode(num2);) ,这样传递int、string类型的参数都没问题。业务要求传递的是图片二进制转化的string类型数据,结果还是报500错误。经过调试对比发现图片二进制数据转化成的string类型数据没有根据url形式传递,而是带有特殊符号的,知道问题所在就好办了,把它转化成有效的url传输数据就行,.net也有现成的封装方法:HttpServerUtility.UrlTokenEncode(bmpBytes),这样500错误也解决了。

测试代码如下:

 1   protected 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数组中
 8             byte[] bmpBytes1 = ms.ToArray();
 9             bmp.Dispose();
10 
11             string bmpBytes = HttpUtility.UrlEncode("bmpBytes") + "=" + HttpServerUtility.UrlTokenEncode(bmpBytes1);
12 
13             string url = "http://192.168.0.28:9800/WebService1.asmx/Send_Image";
14             string a = CallServiceByGet1(url, bmpBytes);
15 
16         }
17         public static string CallServiceByGet1(string strURL,string a)
18         {
19             var result = string.Empty;
20             //创建一个HTTP请求
21             byte[] 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;
35             string srcString;
36             try
37             {
38                 response = (HttpWebResponse)request.GetResponse();//获取http请求的响应对象
39             }
40             catch (WebException ex)
41             {
42                 return 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 
50             return result;
51         }

 

转载于:https://www.cnblogs.com/Connaughtyu/p/10444141.html

你可能感兴趣的:((C#)调用Webservice,提示远程服务器返回错误(500)内部服务器错误)