0

when my below code is executing then some time i am getting The underlying connection was closed exception and root element is missing.

public ShippingAcceptResult ShippingAccept(string acceptDigestCode)
{
    requestType = ShipRequestTypes.ShipAccept;

    xmlRequest = new System.Xml.XmlDocument();
    xmlRequest.LoadXml(@"<?xml version=""1.0""?>
     <ShipmentAcceptRequest>
       <Request>
          <TransactionReference>
             <CustomerContext>TR01</CustomerContext>
             <XpciVersion>1.0001</XpciVersion>
          </TransactionReference>
          <RequestAction>ShipAccept</RequestAction>
          <RequestOption>01</RequestOption>
       </Request>
       <ShipmentDigest>" + acceptDigestCode + "</ShipmentDigest></ShipmentAcceptRequest>");

    byte[] bb = new System.Text.ASCIIEncoding().GetBytes(string.Format(XML_CONNECT, sAccessCode.Trim(), sUserId.Trim(), sPassword.Trim()));

    System.IO.MemoryStream ms = new System.IO.MemoryStream();
    ms.Write(bb, 0, bb.Length);
    xmlRequest.Save(ms);
    bb = ms.ToArray();
    System.Net.WebClient wc = new System.Net.WebClient();
    xmlRespone = new XmlDocument();
    //System.Web.HttpContext.Current.Response.Write(System.Text.ASCIIEncoding.ASCII.GetString(wc.UploadData(UPS_URL, "POST", bb)));
    string serverName = (testMode) ? "wwwcie" : "www";

    serverName = string.Format(UPS_SERVICE_URL, serverName, ShipRequestTypes.ShipAccept);
    xmlRespone.LoadXml(System.Text.ASCIIEncoding.ASCII.GetString(wc.UploadData(serverName, "POST", bb)));
    //xmlRespone.Save(@"d:\shippingresponseAccept.xml");
    return new ShippingAcceptResult(xmlRespone);
}

so i search google and found few SO link there. they are telling to set KeepAlive = false; and i did it. here is the url from where i took the help https://stackoverflow.com/a/24719999/728750

also i checked few other SO link they are saying increase timeout. those url are

https://stackoverflow.com/a/6994391/728750 https://stackoverflow.com/a/3052637/728750 https://stackoverflow.com/a/1530717/728750

var request = (HttpWebRequest)WebRequest.Create(remoteUri);
request.Timeout = 30000;

so i just need to know if i set KeepAlive = false; is it enough or also do i need to set Timeout too along with KeepAlive

please guide me. thanks

Mou
  • 15,673
  • 43
  • 156
  • 275
  • The linked questions seem to explain when timeout is relevant. Was there a particular bit about what timeout does that you are having trouble understanding? – Chris Oct 31 '17 at 14:12
  • i am getting exception "underlying connection was closed" so tell me clearly which option i should use. keepalive=false or set timeout or use both. i am in confusion. guide me. thanks – Mou Oct 31 '17 at 14:29
  • if you know what the two options do you should be able to work out what you need. This is why I am asking if there is something particular about what they do that you don't understand? Do you not understand what the timeout actually does? – Chris Oct 31 '17 at 15:13
  • please tell me briefly what will occur if we set a value for timeout option. now i have not set any timeout in my code. my error is "underlying connection was closed". looking for some lights on it. – Mou Nov 01 '17 at 07:57
  • 1
    I literally just googled "timeout computer meaning" and I got "timeout - Computer Definition. A preset time period during which a given computer task must be completed or the task is canceled. An example is someone configuring his or her Personal Computer to drop the connection to a remote server after a predetermined period passed without some task activity." This seems to perfectly answer your question on what setting the timeout would do. Or you could have just looked at the docs which also explains it https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.timeout(v=vs.110).aspx – Chris Nov 01 '17 at 10:01

0 Answers0