2

I am trying to get a response xml from the UIDAI data centers by sending an xml document using HttpWebRequest and receiving it using HttpWebResponse in C#ASP.net web based application. I already checked it using a java application and it is working fine on their side but the web based application is throwing an exception

An exception of type 'System.Net.WebException' occurred in System.dll but was not handled in user code

Note:The web exception occurs on the 5th line from the last

Please suggest some methods to resolve this

public void SendForAuthentication()//user defined function for Sending and Receiving Requests
{
    string url = "http://auth.uidai.gov.in/1.6/public/9/9/MLTbKYcsgYMq1zgL3WMZYrnyvsarlljxpom2A-QTPc0Zud23shpnqPk";//the host address
    StreamReader sr1 = new StreamReader("D:\\test-signed.xml");//loading xml file to stream
    string XMLData = sr1.ReadToEnd();
    string Conn = @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>";
    XMLData = Conn + XMLData;
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    byte[] requestBytes = System.Text.Encoding.UTF8.GetBytes(XMLData);
    req.Method = "POST";
    req.ContentType = "text/xml;charset=utf-8";
    req.ContentLength = requestBytes.Length;
    Stream requestStream = req.GetRequestStream();
    requestStream.Write(requestBytes, 0, requestBytes.Length);
    requestStream.Close();

    HttpWebResponse response = (HttpWebResponse)req.GetResponse();//**here I am getting a webexception**
    StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);
    string backstr = sr.ReadToEnd();
        reader.ReadToFollowing("title");


    File.WriteAllText("D:\\xml.xml", backstr);


}

edit: The exception I get is "the operation has timed out" though I had increased the operation time

edit2: The above code works fine there was actually a problem with my firewall

  • 1
    What is the `Message` property of the exception? Is there an `InnerException`? – Richard Jul 15 '15 at 07:26
  • @Richard It says that the connection has timed out. I increased the 'httpRuntime executionTimeout="1000000"'.When I tested sending an xml to the same url using a server engine provided by [link](www.requestmaker.com) it worked just fine – prannoy noel Jul 16 '15 at 06:39
  • I would make use of something like Fiddler to see what is going on at the HTTP level: do the request's headers and body match what the service expects. Also, please update the question with the full details of the exception and your debugging steps. – Richard Jul 16 '15 at 07:22
  • When using fiddler,I get 3 processes which matches with the host name. The first one gives a result:200 and says that it is encoded with response headers Entity:content-length:2731 content-type:text/html;charset=utf-8 Miscellaneous: X-SourceFiles: =?UTF-8?B?QzpcVXNlc... The second process also gives a HTTP/1.1 200 OK and the content length is changed and content type to application/json The third one with transport content-encoding:qzip Is there a different way to receive it – prannoy noel Jul 17 '15 at 07:26
  • You need to use the right decoding options in the inspectors: look at the transformer tab of the lower (response) pane – Richard Jul 17 '15 at 08:00
  • It X-sourcefiles is actually showing this "C:\Users\Prannoy Noel Jada\Documents\Visual Studio 2013\WebSites\WebSite2\Default.aspx" The exception is the operation has timed out though I had increased the time Are there any alternate methods for posting. – prannoy noel Jul 17 '15 at 09:07
  • You need to compare in detail the request (headers, body) between a success and a failure. – Richard Jul 17 '15 at 09:44
  • Thanks for the help, It hit me that my office might not allows such http calls to unknown url and on my private network it worked fine – prannoy noel Jul 21 '15 at 21:31

0 Answers0