HttpWebResponse Problems

S

Sean Chapman

ok, heres the problem.. i have an asp.net page that im using to kind of
relay information back and forth. So on the Page_Load i make a request to a
webservice and return some xml back to the first calling app. now... i wrote
a test app to try it out, but i cannot get the proper data from it. It
always says the the ContentLength is -1 so my my StreamReader.ReadToEnd
throws an exception.. heres the chunk of code in the driver app:

webRequest = (HttpWebRequest)WebRequest.Create(webRequestServer);

webRequest.Method = "POST";

webRequest.ContentLength = doc.InnerXml.Length;

webRequest.ContentType = "text/xml";

webRequest.KeepAlive = false;

myWriter = new StreamWriter(webRequest.GetRequestStream());

myWriter.Write(doc.InnerXml.ToString());

myWriter.Close();

webResponse = (HttpWebResponse)webRequest.GetResponse();


StreamReader myReader = new StreamReader(webResponse.GetResponseStream());


responseFromServer = myReader.ReadToEnd();

myReader.Close();



Im thinking the problem is on the asp page. A weird thing i came across is
that it wont let me do a XmlDoc.Save on the response. so what i end up doing
is this:

myXMLDoc.LoadXml("xmlString")

responseXML = myXMLDoc.DocumentElement

Response.Clear()

Response.BufferOutput = True

Response.ContentType = "text/xml"

responseXML.WriteTo(New XmlTextWriter(Response.Output))

Response.Flush()

Response.Close()



I tried adding and appending a ContentLength Header to the response, but no
good.. and you can set the content-type fine. but there is no property for
Content-Length.



so what do i do



thanx for any help
 
V

vMike

Maybe this example will help you.

<%@ Strict=True %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>
<html>
<script language="vb" runat="server">

Sub button_click(sender as object, e as EventArgs)

Dim mgWebRequest As HttpWebRequest
Dim stringPost, stringResult As String
Dim mgStreamWriter As StreamWriter
Dim mgWebResponse As HttpWebResponse
Dim mgStreamReader As StreamReader

'Send Data to other form

mgWebRequest=CType(WebRequest.Create(otherpage.text),HttpWebRequest)

stringPost = senddata.text
if stringPost.length = 0 then
mgWebRequest.Method = "GET"
else
mgWebRequest.Method = "POST"
mgWebRequest.ContentLength = stringPost.length 'length
mgWebRequest.ContentType = "application/x-www-form-urlencoded"
mgStreamWriter = Nothing
mgStreamWriter = New StreamWriter(mgWebRequest.GetRequestStream())
mgStreamWriter.Write(stringPost)
mgStreamWriter.Close()
end if
mgWebResponse = CType(mgWebRequest.GetResponse(),HttpWebResponse)
mgStreamReader = New StreamReader(mgWebResponse.GetResponseStream())
stringResult=(mgStreamReader.ReadToEnd())
mgStreamReader.Close()
response.write(stringResult)
end sub
</script>
<body>
<form runat="server">
Enter site data (including http://)
<asp:textbox id="otherpage" width ="500"
text="http://search.msn.com/results.asp?" runat="server"/><br>
Enter Post Data
<asp:textbox id="senddata" width ="500"
text="RS=CHECKED&FORM=MSNH&v=1&q=shoes" runat="server"/><br>
<asp:button id="send" text="send" onclick="button_click" runat="server"/>
</form>
</body>
</html>
 

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

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top