Kostya Kartavenka
Kostya Kartavenka

Reputation: 45

WebRequest multiple write and read

After I create connection, I need to send IDs and then store response, then send again and again (usually ~60-100 IDs). Currently I did it this way:

public void EThread()
{
    HttpWebRequest HWRequest = (HttpWebRequest)WebRequest.Create(_AjaxURL);
    HWRequest.Method = "POST";
    HWRequest.ContentType = "text/xml; encoding='utf-8'";

    string StrID;

    while(!_RequiredIDs.IsEmpty)
       if (_RequiredIDs.TryDequeue(out StrID))
           Extract(StrID, ref HWRequest);
}

public void Extract(string ID, ref HttpWebRequest HWRequest)
{
    string data = "strID=" + ID;
    byte[] Bytes = Encoding.ASCII.GetBytes(data);

    using (Stream SWriter = HWRequest.GetRequestStream())
    {
        SWriter.Write(Bytes, 0, Bytes.Length);
        SWriter.Close();
    }

    HttpWebResponse resp = (HttpWebResponse)HWRequest.GetResponse();

    using (Stream s = resp.GetResponseStream())
    using (StreamReader sr = new StreamReader(s))
       _RespnseCollection.Enqueue(sr.ReadToEnd());
}

However, I got this error: System.Net.WebException: 'The request was aborted: The connection was closed unexpectedly.. If I put HttpWebRequest inside Extract method it works, but that works long. This was checked on 1 thread. What is wrong with the code or the server does not support this.

Upvotes: 0

Views: 832

Answers (1)

Jens Meinecke
Jens Meinecke

Reputation: 2940

The problem is that you are re-using the HWRequest. But an HttpWebRequest is a single use item. In other words you can fire off the request only once.

So if thr _RequiredIDs contains more than one item, the first item will work properly, but change the state of the HwRequest to completed (ie it now contains a response) and the HaveResponse property will be set to true.

If you then again execute GetRequestStream()or GetResponse() it determines that, having already received a response, it does not make sense to set up the request again.

So you could modify your code as follows:

public void EThread()
{

    string StrID;

    while(!_RequiredIDs.IsEmpty)
       if (_RequiredIDs.TryDequeue(out StrID))
       {
           HttpWebRequest HWRequest = (HttpWebRequest)WebRequest.Create(_AjaxURL);
           HWRequest.Method = "POST";
           HWRequest.ContentType = "text/xml; encoding='utf-8'";
           Extract(StrID, ref HWRequest);
       }
}

BUT, I suppose this is really functionally equivalent to declaring the HttpRequest and initializing inside the Extract method.

SUGGESTIION: Try calling HWRequest.Reset():

public void EThread()
{
    HttpWebRequest HWRequest = (HttpWebRequest)WebRequest.Create(_AjaxURL);
    HWRequest.Method = "POST";
    HWRequest.ContentType = "text/xml; encoding='utf-8'";

    string StrID;

    while(!_RequiredIDs.IsEmpty)
       if (_RequiredIDs.TryDequeue(out StrID))
       {
           Extract(StrID, ref HWRequest);
           HWRequest.Reset();
       }
}

Upvotes: 1

Related Questions