HTTPWebRequest - GetResponse throws exception

P

Peter Feller

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;
}
}
 
P

Peter Feller

Quick followup:

I have worked on this some more and am now getting a slightly
different exception.

The Exception Status is: Receive Failure.
"The underlying connection was closed: An unexpected error occurred on
a receive."

The stack is the same as below.

I can duplicate this error on three different servers. I wonder if it
has something to do with the ISAPI filter. It's usings WriteToClient
to send the data back. It's just really strange that it works using
the MSXML object. What is the difference between IXMLHTTPRequest and
HTTPWebRequest?

And, I can successfully retrieve the contents of HTML pages, when I
update the URI with the additional path info, and of course comment
out the following three lines of code.

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

Then the GetResponse call works!
 
F

Feroze [MSFT]

It is difficult to guess the reason for the exception by just looking at the
code. Your code seems fine. It could be that the server is sending a
malformed response, and HttpWebRequest is choking on it.

Can you get a network sniff of the POST request & response from the server?
That should help figure out the problem.

--
Remove "user" from the email address to reply to the author.

This posting is provided "AS IS" with no warranties, and confers no rights

Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top