(C) call Webservice to prompt remote server to return error (500) internal server error

Keywords: C# encoding Windows

Because the work needs to call the WebService interface, I checked the following data and found that adding a service reference can directly call webservice

Reference address: https://www.cnblogs.com/peterpc/p/4628441.html

What if you don't add service references? Then I went to see how to call webservice according to http protocol and did a nonparametric interface test, as follows:

However, once an interface call with parameters is made, it will prompt 500 errors (remote server returns error (500) internal server error). After half a day's data is checked, most of them say that ContentType = "application/x-www-form-urlencoded; charset=UTF-8"; change to ContentType = "text/html"; or set ValidateRequest="false" in < @ page..% >. The result is the same mistake. Finally, it is found in https://www.jb51.net/article/120015.htm that the parameters are to be spliced (param = HttpUtility.UrlEncode("param1") + "=" + HttpUtility.UrlEncode(num1) + "&" + HttpUtility.UrlEncode("param2") + "=" + HttpUtility.UrlEncode(num2)), so that the parameters of type int and string can be passed without any problem. The business requires to pass the string type data of binary image conversion, and the result is still a 500 error. After debugging and comparison, it is found that the string type data converted from binary image data is not transferred according to the url form, but with special symbols. It's easy to know the problem. Just turn it into effective url transmission data. Net also has a ready-made encapsulation method: HttpServerUtility.UrlTokenEncode(bmpBytes), so 500 errors are also solved.

The test code is as follows:

 1   protected void Page_Load(object sender, EventArgs e)
 2         {
 3             Bitmap bmp = new Bitmap(System.Drawing.Image.FromFile("C:/Users/TYTD/Desktop/Test samples/ch_DJI_279.jpg"));
 4             MemoryStream ms = new MemoryStream();
 5             bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
 6             ms.Flush();
 7             //Save binary data to byte Array
 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             //Create a HTTP request
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();//Obtain http Requested response object
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;   //Return value assignment
48             reader.Close();
49 
50             return result;
51         }

Posted by beanman1 on Wed, 04 Dec 2019 16:14:23 -0800