4

I am writing a program that will submit a XML to a website. The code written works fine, but sometimes it just stops working for some reason, throwing a System.Net.ProtocolViolationException. I can close the program and re-run - it starts working again just fine.

Here is the code that I am using:

private string Summit(string xml)
{
    string result = string.Empty;
    StringBuilder sb = new StringBuilder();
    try {
        WebRequest request = WebRequest.Create(this.targetUrl);
        request.Timeout = 800 * 1000;

        RequestState requestState = new RequestState(xml);
        requestState.Request = request;
        request.ContentType = "text/xml";

        // Set the 'Method' property  to 'POST' to post data to a Uri.
        requestState.Request.Method = "POST";
        requestState.Request.ContentType = "text/xml";

        // Start the Asynchronous 'BeginGetRequestStream' method call.    
        IAsyncResult r = (IAsyncResult)request.BeginGetRequestStream(new AsyncCallback(ReadCallBack), requestState);

        // Pause the current thread until the async operation completes.
        // Console.WriteLine("main thread waiting...");

        allDone.WaitOne();
        // Assign the response object of 'WebRequest' to a 'WebResponse' variable.
        WebResponse response = null;
        try {
            response =request.GetResponse();
        } catch (System.Net.ProtocolViolationException ex) {
            response = null;
            request.Abort();

            request = null;
            requestState = null;
            return "";
        }
        //Console.WriteLine("The string has been posted.");
        //Console.WriteLine("Please wait for the response...");

        Stream streamResponse = response.GetResponseStream();
        StreamReader streamRead = new StreamReader(streamResponse);
        Char[] readBuff = new Char[256];
        int count = streamRead.Read(readBuff, 0, 256);

        //StringBuilder sb = new StringBuilder();
        while (count > 0) {
            String outputData = new String(readBuff, 0, count);
            sb.Append(outputData);
            count = streamRead.Read(readBuff, 0, 256);
        }

        // Close the Stream Object.
        streamResponse.Close();
        streamRead.Close();
        //allDone.WaitOne();

        // Release the HttpWebResponse Resource.
        response.Close();
        //return sb.ToString();
    } catch (WebException webex) {
        Debug.WriteLine(webex.Message);

    } catch (System.Web.Services.Protocols.SoapException soapex) {
        Debug.WriteLine(soapex.Message);
    } catch (System.Net.ProtocolViolationException ex) {
        Debug.WriteLine(ex.Message);
    } catch (Exception ex) {
        Debug.WriteLine(ex.Message);
    }
    return sb.ToString();
}


private static void ReadCallBack(IAsyncResult asyncResult)
{
    try {
        RequestState myRequestState = (RequestState)asyncResult.AsyncState;
        WebRequest myWebRequest2 = myRequestState.Request;

        // End of the Asynchronus request.
        Stream responseStream = myWebRequest2.EndGetRequestStream(asyncResult);

        //Convert  the string into a byte array.
        ASCIIEncoding encoder = new ASCIIEncoding();
        byte[] ByteArray = encoder.GetBytes(myRequestState.Xml);

        // Write data to the stream.
        responseStream.Write(ByteArray, 0, myRequestState.Xml.Length);
        responseStream.Close();                  
    } catch (WebException e) {
        Console.WriteLine("\nReadCallBack Exception raised!");
        Console.WriteLine("\nMessage:{0}", e.Message);
        Console.WriteLine("\nStatus:{0}", e.Status);
    }
    allDone.Set();
}

response =request.GetResponse() is when it fails and gives an error

You must provide a request body if you set ContentLength>0 or SendChunked==true. Do this by calling [Begin]GetRequestStream before [Begin]GetResponse.

Any help would be greatly appreciated.

Liam
  • 27,717
  • 28
  • 128
  • 190
user2664502
  • 85
  • 2
  • 2
  • 5
  • I guess you should provide the request with a contentLength such as : http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.contentlength.aspx http://msdn.microsoft.com/en-us/library/system.net.webrequest.contentlength.aspx – Ktt Aug 08 '13 at 13:50
  • Hi, I have tried that also .. but no luck .. Thank you – user2664502 Aug 12 '13 at 14:27

1 Answers1

6

This gets tricky since we're doing async calls.

Do this in the following order:

request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request)

Then in 'GetRequestStreamCallback(IAsyncResult asynchronousResult)' call:

request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request)

Lastly, in the GetResponse, be sure to close the stream:

response.Close();
allDone.Set();

MSDN Does a really good job explaining it: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetrequeststream.aspx

Irsal
  • 156
  • 6
  • RE: Content Length "Any value other than -1 in the ContentLength property indicates that the request uploads data and that only methods that upload data are allowed to be set in the Method property." – Irsal Aug 08 '13 at 13:58
  • Hi Irsal.. Now this seems to be working much better .. And is not falling over as frequently as it was earlier.. But I am going to test it more thoroughly... Thank you so much for the help.. – user2664502 Aug 12 '13 at 14:28
  • Great! Let me know if you run into anything odd during those tests. – Irsal Aug 12 '13 at 14:30