HTTPWebRequest - GetResponse throws exception

From: Peter Feller (pfeller_at_cox.net)
Date: 03/04/04


Date: 4 Mar 2004 12:10:26 -0800

Hello,

I am trying to make a HTTP request to a component on a server. In the
past, our client applications have used the XMLHTTPRequest object in
MSXML to make the call. See ‘OLD CODE'. The call is intercepted by an
ISAPI filter which forwards the request to the appropriate application
server and then responds. This works fine.

For our next generation clients, I wanted to make the call using the
HTTPWebRequest or WebClient class provided by .NET. I then would no
longer have to rely on an unmanaged COM object and have only .NET
assemblies in the applications. That way I could take advantage of the
.NET deployment options.

The problem I am having is that the function I wrote to make the
requests (see NEW CODE) does not work. I can create the request object
and write the data to the server, but when I call the following line
of code I get an exception (see below).
Dim resp As HttpWebResponse = CType(myReq.GetResponse(),
HttpWebResponse)

Following Exception is thrown, where the WebExcpeption.Status =
WebExceptionStatus.ServerProtocolViolation

The underlying connection was closed: The server committed an HTTP
protocol violation.

   at System.Net.HttpWebRequest.CheckFinalStatus()
   at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult
asyncResult)
   at System.Net.HttpWebRequest.GetResponse()
   at OMNI_Common.MakeCISRequest.MakeWebRequest(String URL, String
strRequest, String& strOut) in
C:\Projects\OMNI\Frontend\DLL\OMNI_Common\Common
Classes\MakeCISRequest.vb:line 258

When I enable logging in my ISAPI filter, I see that the request has
been processed and the data is being sent back to the client. The
problem seems to be that the client can not process the returned data.

I have played with the following many properties and the encoding
scheme, but can't seem to get anywhere. Besides the ‘The underlying
connection was closed' error I have also received the 405 error
(Invalid Method).

Any feedback would be greatly appreciated.

-Peter

NEW CODE.

Public Sub MakeWebRequest(ByVal URL As String, ByVal strRequest As
String, ByRef strOut As String)
        Try
           Dim myReq As HttpWebRequest = CType(WebRequest.Create(URL),
HttpWebRequest)
           myReq.KeepAlive = True
           myReq.Expect = ""
           myReq.Accept = "*/*"
           myReq.AllowAutoRedirect = True
           myReq.Method = "POST"

           Dim POSTBuffer() As Byte =
System.Text.Encoding.UTF8.GetBytes(strRequest)
           myReq.ContentLength = POSTBuffer.Length
           myReq.ContentType = "text/xml"
           'myReq.AllowWriteStreamBuffering = True
           'myReq.SendChunked = True
           myReq.Timeout = 10000
           myReq.Headers.Add("Request-Type", "ALifeRequest")

           Dim DataStream As Stream = myReq.GetRequestStream()
           DataStream.Write(POSTBuffer, 0, POSTBuffer.Length)
           DataStream.Close()

           ‘''''''''''''''''''' THIS LINE CAUSES EXCEPTION
           Dim resp As HttpWebResponse = CType(myReq.GetResponse(),
HttpWebResponse)
           ‘''''''''''''''''''''''''''''''
           Dim sr As StreamReader = New
StreamReader(resp.GetResponseStream())
           strOut = sr.ReadToEnd()
           sr.Close()

        Catch WebExcp As WebException
           If (WebExcp.Status = WebExceptionStatus.ProtocolError) Then
                Dim wr As HttpWebResponse = WebExcp.Response
                If (401 = wr.StatusCode) Then
                ElseIf (407 = wr.StatusCode) Then
                End If

            End If
            Dim wr1 As HttpWebResponse = WebExcp.Response
        Finally
            If strOut.IndexOf("ERRORS") > -1 Then
                Throw New CCMException(strOut)
            End If
        End Try

    End Sub

End Class

OLD CODE.

HRESULT CSendHTTPRequestThread::Work(CString &xmlOut, CString
*strTime)
{
        CString strMethod("CSendHTTPRequestThread::Work");

        CComVariant varXMLdoc(m_bstrRequest);
        CComVariant varAsync(m_bAsync);
        CComVariant varEmpty(L"");
        MSXML2::IXMLHTTPRequestPtr m_pHTTP = NULL;
        HRESULT hr = m_pHTTP.CreateInstance("Msxml2.XMLHTTP.4.0");
        m_pHTTP->open (L"POST", m_bstrURL, varAsync, varEmpty, varEmpty);
        m_pHTTP->setRequestHeader(L"Content-type", L"text/xml");
        m_pHTTP->setRequestHeader(L"Request-Type", L"ALifeRequest");
        m_pHTTP->send (varXMLdoc);

        m_pHTTP->get_readyState(&lReadyState);
        if (lReadyState == 4 ) //successful
        {
                BSTR bstrXmlDocMaster = NULL;
                m_pHTTP->get_responseText(&bstrXmlDocMaster);
                xmlOut = CString (bstrXmlDocMaster,
::SysStringLen(bstrXmlDocMaster));
                ::SysFreeString (bstrXmlDocMaster);
                BSTR bstrStat;
                long lVal;
                m_pHTTP->get_status(&lVal);
                m_pHTTP->get_statusText(&bstrStat);

                return S_OK;
        }
}



Relevant Pages

  • Re: breaking the model
    ... > The forms data then is in the Request object. ... HTTP Request; in this case, the form POST Request from the Page. ... client and server. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Resolving record with enumerated type
    ... In a CPU BFM package, ... because data goes in two directions (request from the ... from the server to the client), you'll need some way to orchestrate ...
    (comp.lang.vhdl)
  • Each client gets the same port number
    ... This class creates a server socket that will ... For each new connection request from client ... */ private String calculate{String result = null; ...
    (comp.lang.java.help)
  • Re: pygame and socket.recv
    ... player, and for the communication, I am sending a pickle string across ... The client sends a dictionary to the server. ... time.sleep to keep the frame rate at 40 frames per second. ...
    (comp.lang.python)
  • Re: Kerberos with Windows Integrated authentication
    ... behaviour if your Web server is in the client broweser's Internet zone. ... referencing it by computer name rather than FQDN), the browser will request ... Obviously, if you want to use Kerberos for authentication, you will either ...
    (microsoft.public.windows.server.security)