HttpWebRequest from winform

  • Thread starter Thread starter Brian Brown
  • Start date Start date
B

Brian Brown

I have code which works as an asp.net page that posts an
xml file to web page and gets a response back.

When the the calls GetResponse() it goes into the page
it's posting to to and works fine. When it's been ported
to a winform it doesn't work on the GetResponse() call.

I think it probably needs credentials but not sure what
to use, tried a few ids w/o any luck.

The asp.net page as well as the winform are on the same
server for now but will later be on seperate servers.

Thanks,

Brian
 
Brian Brown said:
I have code which works as an asp.net page that posts an
xml file to web page and gets a response back.

When the the calls GetResponse() it goes into the page
it's posting to to and works fine. When it's been ported
to a winform it doesn't work on the GetResponse() call.

I think it probably needs credentials but not sure what
to use, tried a few ids w/o any luck.

The asp.net page as well as the winform are on the same
server for now but will later be on seperate servers.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
Web page code that works:

Imports System.IO
Imports System.Xml
Imports System.Xml.Schema
Imports System.Net.CredentialCache
Imports System.Text
Imports System.net

Public Class Class1
Private Sub SendDoc(ByRef sData As String)
Const k_send_to_url As String
= "http://localhost/Reader/Receive.aspx"

Dim request_obj As HttpWebRequest
Dim request_stream As Stream
Dim response_obj As HttpWebResponse
Dim xml_reader As XmlTextReader
Dim docobj As New XmlDocument


Try
request_obj = CType(WebRequest.Create
(k_send_to_url), HttpWebRequest)

request_obj.Method = "POST"
request_obj.ContentType = "text/xml"
request_obj.Timeout = 999999999
request_obj.AllowWriteStreamBuffering = True

request_stream = request_obj.GetRequestStream
()

docobj.LoadXml(sData)

docobj.Save(request_stream)

request_stream.Close()
docobj = Nothing

response_obj = request_obj.GetResponse()

xml_reader = New XmlTextReader
(response_obj.GetResponseStream())
docobj = New XmlDocument
docobj.Load(xml_reader)

request_obj = Nothing
request_stream = Nothing
response_obj = Nothing

Catch ex As Exception
Exit Sub
End Try

End Sub


End Class

Windows code that doesn't:


Imports System.IO
Imports System.Xml
Imports System.Xml.Schema
Imports System.Net.CredentialCache
Imports System.Text
Imports System.net

Public Class Class1

Private Sub SendDoc(ByRef sData As String)
Const k_send_to_url As String
= "http://localhost/Reader/Receive.aspx"

Dim request_obj As HttpWebRequest
Dim request_stream As Stream
Dim response_obj As HttpWebResponse
Dim xml_reader As XmlTextReader
Dim docobj As New XmlDocument


Try


request_obj = CType(WebRequest.Create
(k_send_to_url), HttpWebRequest)

request_obj.Method = "POST"
request_obj.ContentType = "text/xml"
request_obj.Timeout = 999999999
request_obj.AllowWriteStreamBuffering = True

request_stream = request_obj.GetRequestStream
()

docobj.LoadXml(sData)

docobj.Save(request_stream)

request_stream.Close()
docobj = Nothing

response_obj = request_obj.GetResponse()

xml_reader = New XmlTextReader
(response_obj.GetResponseStream())
docobj = New XmlDocument
docobj.Load(xml_reader)

request_obj = Nothing
request_stream = Nothing
response_obj = Nothing

Catch ex As Exception
Exit Sub
End Try
End Sub
End Class
 
Windows code that doesn't:

And what happens, exactly? I notice that when you catch an exception,
you don't do anything with it - is an exception being thrown? If so,
what's the contents of the exception?
 
When the response_obj = request_obj.GetResponse()

code executes if you put a breakpoint on it, it should
post the xml file to the page defined in k_send_to_url.

This happens in the web version but not in the windows
version which I intend to turn into a windows service.

No error is thrown until I try to use the result which
should be an xml document.

It has to be security somehow I guess, but can't seem to
figure out what. I tried setting credentials and it still
didn't work.

Thanks!
 
When the response_obj = request_obj.GetResponse()

code executes if you put a breakpoint on it, it should
post the xml file to the page defined in k_send_to_url.

This happens in the web version but not in the windows
version which I intend to turn into a windows service.

No error is thrown until I try to use the result which
should be an xml document.

It has to be security somehow I guess, but can't seem to
figure out what. I tried setting credentials and it still
didn't work.

You haven't said what *does* happen in the Windows version though. What
happens you you execute request_obj.GetResponse()? What does it return?
Does it block? Does it throw an exception?
 
Nothing happens, no error, it doesn't go into the other
page, the error is on the next line when it expects an
xml document and there's nothing.
 
Nothing happens, no error, it doesn't go into the other
page, the error is on the next line when it expects an
xml document and there's nothing.

I suggest you look at the contents of the stream then, and find out
what's in there. Also look at your webserver logs to find out whether
it thinks it sent any content back - and preferably what it thought it
received, too.
 
It doesn't even go into the page. I have a breakpoint
where in the asp page it goes into the calling page, the
windows one doesn't even go into the page.
 
It doesn't even go into the page. I have a breakpoint
where in the asp page it goes into the calling page, the
windows one doesn't even go into the page.

And yet GetResponse returns with no exception? What status does the
returned response have?
 
HttpWebResponse.statusmessage = ok

same for HttpWebResponse.statuscode

yet it never went into the page, nor does it contain the
returned content.

In that case, I suggest you find what it *did* go to. Does it contain
*any* content? My guess is that perhaps a proxy is getting in the way
unexpectedly.

Try using a network sniffer to find out what's going on.
 
Back
Top