.NET Web Service Receives HTTP POST Request (500) Internal Server Error

I am currently writing a C # web service that has several methods, one of which should accept POST HTTP requests. The first thing I did was modify the web.config file in the web service project as shown below.

<webServices>
  <protocols>
    <add name="HttpSoap"/>
    <add name="HttpPost"/>
    <add name="HttpPostLocalhost"/>
    <add name="Documentation"/>
  </protocols>
</webServices>

I can start the web service locally, and when I click on the method in the browser, I see that it processes HTTP POST requests and accepts args = string, since my signature on the web method takes one string parameter, args. I then test this through the ASP.NET test application, using the code below to run the HTTP POST request.

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(ConfigurationManager.AppSettings["PaymentHubURL"].ToString());

request.KeepAlive = false;
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";

StringBuilder sb = new StringBuilder();
sb.Append("message_type=");
sb.Append(HttpUtility.UrlEncode("Txn_Response"));

byte[] bytes = UTF8Encoding.UTF8.GetBytes(sb.ToString());
request.ContentLength = bytes.Length;
using (Stream postStream = request.GetRequestStream())
{
    postStream.Write(bytes, 0, bytes.Length);
}

string test;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
     StreamReader reader = new StreamReader(response.GetResponseStream());

     test = reader.ReadToEnd();
}

, " : (500)" ". , stringbuilder , -, . , , , . string [] -, .

- ?

+3
2

. Microsoft -, : " -" " ".

, "- asmx", "-" . , .

- "". - . -. .

, .

+2

, HttpPost.

, , :

http://msdn.microsoft.com/en-us/library/debx8sh9.aspx

, , unicode char value

,

 System.Text.Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
    StreamReader objSR;

webResponse = (HttpWebResponse)response.GetResponse();
StreamReader reader = webResponse.GetResponseStream();
objSR = new StreamReader(objStream, encode, true);
sResponse = objSR.ReadToEnd();
0

Source: https://habr.com/ru/post/1774972/


All Articles