System.Net.WebException: The remote server returned an error: (500) Internal Server Error.

Discussion in 'ASP .Net' started by kuladeep.mohan@gmail.com, Mar 24, 2006.

  1. Guest

    Hi
    I am trying to consume a web service from asp.net web page through
    HTTP POST method and I am getting the following error message

    "System.Net.WebException: The remote server returned an error: (500)
    Internal Server Error."


    I have been searchign the web and followed all the options which worked

    for others


    1. Changed the web.config of the web service to accomodate HttpPost
    <webServices>
    <protocols>
    <add name="HttpGet"/>
    <add name="HttpPost"/>
    </protocols>
    </webServices>


    2. Used the HttpRequest.Credentials =
    CredentialCache.DefaultCredentials;


    I am not sure what is wrong there it fails while the request is sent to

    the web service. The web service works fine with other web clients


    Let me know your ideas


    thanks
    Mohan
    , Mar 24, 2006
    #1
    1. Advertising

  2. sirfunusa Guest

    Other web clients? What is different? Run the debugger on the server,
    set a breakpoint and POST from your client, and step through the code
    sirfunusa, Mar 24, 2006
    #2
    1. Advertising

  3. Guest

    sirfunusa wrote:
    > Other web clients? What is different? Run the debugger on the server,
    > set a breakpoint and POST from your client, and step through the code


    Other clients I mean when I reference the web service and access the
    web method it works fine...But when I use an ASP.NET page with the
    HTTPReuest Object as shown below it throws the error


    "System.Net.WebException: The remote server returned an error: (500)
    Internal Server Error."

    Code :

    HttpWebRequest wr =
    (HttpWebRequest)WebRequest.Create("http://localhost//SubtractNumbers//Service1.asmx//Subtract");




    wr.Method = "POST";
    wr.ContentType = "application/x-www-form-urlencoded";
    //wr.ContentLength = bytes.Length;

    StreamWriter writer = new
    StreamWriter(wr.GetRequestStream(),System.Text.Encoding.UTF8);
    // // Write the xml text into the stream
    writer.WriteLine("Hi");
    writer.Close();


    WebResponse wrs = wr.GetResponse();
    Stream strm = wrs.GetResponseStream();
    StreamReader sr = new StreamReader(strm);
    String line;
    while( (line = sr.ReadLine()) != null)
    Response.Write(line);
    strm.Close();
    , Mar 24, 2006
    #3
  4. Guest

    sirfunusa wrote:
    > Other web clients? What is different? Run the debugger on the server,
    > set a breakpoint and POST from your client, and step through the code


    Other clients I mean when I reference the web service and access the
    web method it works fine...But when I use an ASP.NET page with the
    HTTPReuest Object as shown below it throws the error


    "System.Net.WebException: The remote server returned an error: (500)
    Internal Server Error."

    Code :

    HttpWebRequest wr =
    (HttpWebRequest)WebRequest.Create("http://localhost//SubtractNumbers//Service1.asmx//Subtract");




    wr.Method = "POST";
    wr.ContentType = "application/x-www-form-urlencoded";
    //wr.ContentLength = bytes.Length;

    StreamWriter writer = new
    StreamWriter(wr.GetRequestStream(),System.Text.Encoding.UTF8);
    // // Write the xml text into the stream
    writer.WriteLine("Hi");
    writer.Close();


    WebResponse wrs = wr.GetResponse();
    Stream strm = wrs.GetResponseStream();
    StreamReader sr = new StreamReader(strm);
    String line;
    while( (line = sr.ReadLine()) != null)
    Response.Write(line);
    strm.Close();
    , Mar 24, 2006
    #4
  5. q Guest

    Are you sending the proper XML message (SOAP)? If your message isn't
    formatted properly, you will definately get a server 500 error back.
    It looks liek you are actually just sending text ("Hi"). That's not a
    proper SOAP message. Here is a sample of a SOAP message:
    http://www.w3.org/2004/06/03-google-soap-wsdl.html .

    I personally much of web services manually because it's just easier to
    interop with COM that way.

    Here's part of what I do... (also be sure to set your SOAPAction in the
    http header!) messageData hold my XML message I'm sending.

    ASCIIEncoding encoding = new ASCIIEncoding( );
    byte[] buffer = encoding.GetBytes(messageData);

    HttpWebRequest myRequest =
    (HttpWebRequest)WebRequest.Create(this.EndPoint);
    myRequest.Method = "POST";
    myRequest.ContentType = "text/xml";
    myRequest.ContentLength = buffer.Length;
    myRequest.Headers.Add(String.Format("SOAPAction: \"{0}\"",
    operationName));
    myRequest.Timeout = 10000;

    using (Stream newStream = myRequest.GetRequestStream( )) {
    newStream.Write(buffer, 0, buffer.Length);
    newStream.Close( );
    }

    HttpWebResponse myHttpWebResponse =
    (HttpWebResponse)myRequest.GetResponse( );

    string rawResponse =
    GetWebResponseString(myHttpWebResponse);

    myHttpWebResponse.Close( );

    Here is the GetWebResponseString method...

    private static string GetWebResponseString(WebResponse
    myHttpWebResponse) {
    StringBuilder rawResponse = new StringBuilder( );
    using (Stream streamResponse =
    myHttpWebResponse.GetResponseStream( )) {
    using (StreamReader streamRead = new
    StreamReader(streamResponse)) {
    Char[] readBuffer = new Char[256];
    int count = streamRead.Read(readBuffer, 0, 256);

    while (count > 0) {
    String resultData = new String(readBuffer, 0,
    count);
    rawResponse.Append(resultData);
    count = streamRead.Read(readBuffer, 0, 256);
    }
    }
    }
    return rawResponse.ToString( );
    }

    Look closely at what is returned. You may actually be getting the
    reason for the fault in the message.

    Also, if all is successful, you will get an XML message back. Are you
    prepared to parse it?

    You may want to actually use the .NET web service mechanism if you
    don't want to do everything manually. Since you aren't using the .NET
    web service mechanism the web.config configuration doesn't apply.

    If you are intending to use .NET for web services, then all you have to
    do is add the WSDL file as a web reference and use it as though it were
    local. There's not really much work to it. If that is the case then
    everything I said above is void, since this is the manual way to do it.
    q, Mar 24, 2006
    #5
  6. cpdarkman

    Joined:
    Aug 26, 2009
    Messages:
    1
    Check Exception

    The server on SOAP can return a 500 Internal Response Error and throw an exception.
    When you catch the exception, you can check to see if a Response exists and grab it from there. Fiddler2 should also be able to give you a clue as what the response is.


    ...
    catch (System.Net.WebException ex)
    {
    String responseFromServer = ex.Message.ToString() + " ";
    if (ex.Response != null)
    {
    using (WebResponse response = ex.Response)
    {
    Stream data = response.GetResponseStream();
    using (StreamReader reader = new StreamReader(data))
    {
    responseFromServer += reader.ReadToEnd();
    }
    }
    }

    }
    cpdarkman, Aug 26, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGFubnk=?=
    Replies:
    1
    Views:
    1,096
    Joerg Jooss
    Dec 3, 2005
  2. BobbyOwens
    Replies:
    0
    Views:
    4,582
    BobbyOwens
    Jan 23, 2009
  3. Jeremy Ames
    Replies:
    1
    Views:
    246
    Jeremy Ames
    Jan 8, 2004
  4. Lisa Ngo
    Replies:
    3
    Views:
    439
    Jan Tielens
    Jan 18, 2004
  5. Replies:
    0
    Views:
    702
Loading...

Share This Page