.NET Web Service receive HTTP POST request (500) Internal Server Error

18,721

I would suggest that you reconsider your approach. Microsoft has written pretty awesome libraries for consuming web services, but there are two ways to do it - "add web reference" and "add service reference".

In your case, it seems you have an "asmx web service" so I would recommend that you add a "web reference" to you project in visual studio. This is assuming you are using visual studio.

After you add this web reference, you can create your client by "new"-ing it. You can the execute any web method on this client. This is the easiest way to consume web services. You do not have to deal with any http complications.

Hope this helps.

Share:
18,721
Neil
Author by

Neil

Updated on June 05, 2022

Comments

  • Neil
    Neil almost 2 years

    I am currently writing a C# web service which has several methods, one of which has to receive HTTP POST requests. The first thing i have done is alter the web.config file in the web service project as below.

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

    I can run the web service locally and when i click on the method in the browser, i can see it handles HTTP POST requests and accepts args=string, as my signature of the web method accepts one string parameter named args. I am then testing this via a test ASP.NET app using the code below to fire 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();
    }
    

    But when i run this i get "The remote server returned an error: (500) Internal Server Error". If i remove the parameter, by removing the stringbuilder and byte code, as well as having no parameter in the web service, it works. So it is obviously a problem with the parameters. I actually want to send more data, and was using a string[] parameter in the web service, but this also failed.

    Can anyone help??

  • Neil
    Neil over 13 years
    That's exactly how i am building it, except i am using the 'using' keyword so i don't need to close my streams.
  • Neil
    Neil over 13 years
    I should have said, it falls over on the second using statement when the following statement is run.
  • Neil
    Neil over 13 years
    HttpWebResponse response = (HttpWebResponse)request.GetResponse()