HttpWebRequest object.GetResponse() fails, timeout. RRS feed

  • Question

  • Hello I am new to programming and I can't seem to figure out why my HttpWebRequest Object.GetResponse() keeps on failing on me. Any advice on why this is happening would be greatly appreciated.  


    I am closing both the Stream object and the HttpWebResponse object after I am done using them. I am stumped.


     public XmlNode AddPacketHelperFunction(HttpWebRequest request, string user, string password, string packetName)


            //create the xml for the packet

            request = InitializeHttpRequestHeaderPacketPost(request, user, password);

            XmlDocument xmlDoc = new XmlDocument();

            XmlNode ndPacket = xmlDoc.CreateElement("packet");

            ndPacket.AppendChild(xmlDoc.CreateElement("name")).InnerText = packetName;

            byte[] packetBytes = Encoding.UTF8.GetBytes(ndPacket.OuterXml);

            request.ContentLength = packetBytes.Length;

            Stream requestStream = request.GetRequestStream();

            requestStream.Write(packetBytes, 0, packetBytes.Length);




    //It keeps on timing out on the call below.

                HttpWebResponse response = request.GetResponse() as HttpWebResponse;




             catch (WebException e)



                throw new TestPacketException("Exception Thrown While Adding Packet");



            catch (Exception e)



                throw new TestPacketException("Exception Thrown While Adding Packet");


            return ndPacket;




     public HttpWebRequest InitializeHttpRequestHeaderPacketPost(HttpWebRequest request, string user, string password)


            request = WebRequest.Create(connectionString) as HttpWebRequest;

            if (request == null)

                Assert.Fail("Request not instantiated. Not sure why. Probably solar radiation.");

            request.Method = "POST";

            request.ContentLength = 0;

            request.ContentType = "text/xml";

            request.Credentials = new NetworkCredential(user, password);

            return request;




    Here are the two function being called.

    drew goldberg
    Saturday, May 21, 2011 2:26 AM

All replies

  • The code looks good. It seems either request isn't received by the server or it doesn't respond (could be some wrong proxy or DNS settings or just plain wrong URI). Try opening the browser on the client and typing the server URI. Does that work?

    Wednesday, July 27, 2011 7:34 AM