[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.sdk

WebRequest.GetRequestStream method

Tim

7/8/2003 2:32:00 PM

I haven't seen too many examples documented using the
GetRequestStream method. I'm not sure if I'm going about
this right, but the following code fails on the last line,
with the error saying "the stream was not available."
What am I doing wrong? Thanks.

------------------------------------------------------
Dim site As Uri = New Uri("http://www.hostserver...)

Dim wReq As WebRequest = WebRequest.Create(site)
wReq.PreAuthenticate = True

Dim cred As NetworkCredential = New NetworkCredential
("myusername", "mypassword")
wReq.Credentials = cred

Dim reqStream As Stream = wReq.GetRequestStream
Dim writer As StreamWriter = New StreamWriter(reqStream,
Encoding.ASCII)
-------------------------------------------------------

Tim
2 Answers

xinhuang

7/9/2003 9:14:00 AM

0

You may take a look at this thread:
<http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threa...
RCHA.216%40tkmsftngp11&rnum=1&prev=/groups%3Fq%3DHow%2Bdo%2BI%2Bdo%2Ba%2BPOS
T%2Bvia%2BHttpWebRequest%253F%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26
selm%3DuqtFNPORCHA.216%2540tkmsftngp11%26rnum%3D1>.

There''s some VB.NET code showing how to POST using the
WebRequest.GetRequestStream method.

Hope this helps.

Regards,
Xin

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. (c) 2003 Microsoft Corporation. All
rights reserved.

Dino Chiesa [MSFT]

7/9/2003 1:46:00 PM

0

What are you trying to do?
If you are trying to retrieve content from http://www.host..., then
you are grabbing the wrong stream.
You want the response stream, not the request stream.

Here''s a working example (sorry C#)

private String GetURI(string URI) {
WebRequest myWebRequest = WebRequest.Create(URI);
WebResponse response = myWebRequest.GetResponse();
StreamReader reader = new StreamReader (response.GetResponseStream());
return reader.ReadToEnd().Trim();
}

if you are trying to post to a site, this works:

private string PostURI(string URI, string Parameters)
{
string CommandURI = URI;
WebRequest myWebRequest = WebRequest.Create(CommandURI);

//needed only if outbound traffic must go through a proxy server
//WebProxy proxyObject = new WebProxy("http://proxyserver:80/",true);
//myWebRequest.Proxy = proxyObject;

myWebRequest.ContentType = "application/x-www-form-urlencoded";
myWebRequest.Method = "POST";
byte [] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters);
myWebRequest.ContentLength = bytes.Length;
Stream OutputStream = myWebRequest.GetRequestStream ();
OutputStream.Write (bytes, 0, bytes.Length);
OutputStream.Close ();
WebResponse MyWebResponse = myWebRequest.GetResponse();
Stream MyStream = MyWebResponse.GetResponseStream();
StreamReader MyStreamReader = new StreamReader(MyStream);
return MyStreamReader.ReadToEnd().Trim();
}



"Tim" <tim.falkins@enforcer.com> wrote in message
news:88bc01c3455d$aad97940$a401280a@phx.gbl...
> I haven''t seen too many examples documented using the
> GetRequestStream method. I''m not sure if I''m going about
> this right, but the following code fails on the last line,
> with the error saying "the stream was not available."
> What am I doing wrong? Thanks.
>
> ------------------------------------------------------
> Dim site As Uri = New Uri("http://www.host...")
>
> Dim wReq As WebRequest = WebRequest.Create(site)
> wReq.PreAuthenticate = True
>
> Dim cred As NetworkCredential = New NetworkCredential
> ("myusername", "mypassword")
> wReq.Credentials = cred
>
> Dim reqStream As Stream = wReq.GetRequestStream
> Dim writer As StreamWriter = New StreamWriter(reqStream,
> Encoding.ASCII)
> -------------------------------------------------------
>
> Tim