欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-04-28 12:44:03
因为工作需要调用WebService接口,查了下资料,发现添加服务引用可以直接调用websevice 参考地址:https://www.cnblogs.com/peterpc/p/4628441.html 如果不添加服务引用又怎么做呢?于是又去查看怎么根据http协议调用webservice并做了个 ......

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

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

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

但一做有参的接口调用就提示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         }