none
Remote Server returns error 401 Unauthorized Webexception (POST) RRS feed

  • Question

  • Hey,

    I'm trying to solve an issue that I Mostly (70%) have (30% is succesfull).

    I trying to do a webrequest (POST) with the following code:

     private static string HttpWebRequest(string busStopCode)
            {
                //XML input
                string xml = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?><Siri version='1.0' xmlns='http://www.siri.org.uk/'><ServiceRequest><RequestTimestamp>2011-10-24T15:09:12Z</RequestTimestamp><RequestorRef><username></RequestorRef><StopMonitoringRequest version='1.0'><RequestTimestamp>2011-10-24T15:09:12Z</RequestTimestamp><MessageIdentifier>12345</MessageIdentifier><MonitoringRef>" + busStopCode + "</MonitoringRef></StopMonitoringRequest></ServiceRequest></Siri>";
                string responseFromServer = null;
    
                // Create a request using a URL that can receive a post. 
                WebRequest request = WebRequest.Create("http://<username>:<password>@nextbus.mxdata.co.uk/nextbuses/1.0/1");
                // Set the Method property of the request to POST.
                request.Method = "POST";
                request.Credentials = CredentialCache.DefaultNetworkCredentials;
                // Create POST data and convert it to a byte array.
                string postData = xml;
    
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
    
                // Set the ContentType property of the WebRequest.
                request.ContentType = "application/x-www-form-urlencoded";
    
                // Set the ContentLength property of the WebRequest.
                request.ContentLength = byteArray.Length;
    
                // Get the request stream.
                Stream dataStream = request.GetRequestStream();
    
                // Write the data to the request stream.
                dataStream.Write(byteArray, 0, byteArray.Length);
    
                // Close the Stream object.
                dataStream.Close();
    
                // Get the response.
    
                WebResponse response = null;
    
                while (response == null)
                {
                    try
                    {
                        response = request.GetResponse();
                    }
                    catch (Exception e)
                    {
                        //MessageBox.Show(e.ToString());
                        Debug.WriteLine(e.ToString());
                    }
                }
                // Display the status.
                //MessageBox.Show(((HttpWebResponse)response).StatusDescription + " Completed");
    
                // Get the stream containing content returned by the server.
                dataStream = response.GetResponseStream();
    
                // Open the stream using a StreamReader for easy access.
                StreamReader reader = new StreamReader(dataStream);
    
                // Read the content.
                responseFromServer = reader.ReadToEnd();
    
                // Clean up the streams.
                reader.Close();
                dataStream.Close();
                response.Close();
    
                return responseFromServer;
            }


    When I call this function I get mostly a messagebox of my exception with:

    "System.Net.WebException: The remote Server returned an error(401) not authorized with System.Net.Http.Webrequest.GetResponse() with WindowsFormApplication1.Form1.HttpWebRequest(string BusstopCode) in <my pathfile>....

    What I'm doiing wrong?

    I already tried several solutions from previous threads but without success...

    Thanks!






    • Edited by Eclectica Monday, April 15, 2013 1:17 PM
    Monday, April 15, 2013 1:06 PM

Answers

  • Hi,

    I already found the problem.

    I researched the network via Wireshark and found a 401/1 error.

    And fixed with a Digest Authententication class

    • Marked as answer by Eclectica Monday, April 22, 2013 8:37 AM
    Monday, April 22, 2013 8:37 AM

All replies

  • On 4/15/2013 9:06 AM, Eclectica wrote:
     <snipped>

    Either the site doesn't exist, the URL is bad or the XML is malformed when you get a 401.

    Monday, April 15, 2013 1:23 PM
  • The site doesn't in this forum is not valid because the username and password are removed and you cannot access the site by copy it in your webbrowsers.

    And if the URL is bad why does it works 30% ?

    Monday, April 15, 2013 1:28 PM
  • On 4/15/2013 9:28 AM, Eclectica wrote:

    The site doesn't in this forum is not valid because the username and password are removed and you cannot access the site by copy it in your webbrowsers.

    And if the URL is bad why does it works 30% ?

    Maybe because 30% of the time you are sending well formed-XML, instead of mal-formed XML 70% of the time?

    You can always go into debug mode and set a breakpoint to stop and see what is in  (string postData = xml);

    You can copy the string XML into a  test.xml file with NotePad, save the file and access the file with a browser. If the XML is mal-formed, the browser is going to tell you that.

    Monday, April 15, 2013 3:09 PM
  • Hi Eclectica,

    The code you provided looks good which simply post some form paramters to the target endpoint and read the response stream. I think it is possible that the server-side has occured some problems such as some unhandled exceptions thrown. Sometimes 401 error doesn't exact means authentication issues and it is possibly caused by other exceptions occurs before the web/http layer handles the response. Therefore, if convenient, you can try perform some troubleshooting at server-side to see what is the detailed error occured there.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, April 22, 2013 5:13 AM
    Moderator
  • Hi,

    I already found the problem.

    I researched the network via Wireshark and found a 401/1 error.

    And fixed with a Digest Authententication class

    • Marked as answer by Eclectica Monday, April 22, 2013 8:37 AM
    Monday, April 22, 2013 8:37 AM
  • On 4/22/2013 4:37 AM, Eclectica wrote:

    Hi,



    I already found the problem.



    I researched the network via Wireshark and found a 401/1 error.

    And fixed with a Digest Authententication class

    The next time you should post the exact error code. 401.1 is known by just about every Web developer.

    Monday, April 22, 2013 9:43 AM
  • Did not know that error from the beginning :) Visual studio gave me only 401.

    After I tried it with wireshark the error was more detailled

    Monday, April 22, 2013 9:44 AM