HttpWebRequest To send POST HTTP request and Read...

M

ME

Hi;

I am getting "Unhandled Exception:
System.Net.WebException: The remote server returned an erro
r: (401) Unauthorized."


when I am trying to get a page via post. Code follows...




System.Text.ASCIIEncoding encoding = new
System.Text.ASCIIEncoding();
string postData="age=23&state=47";
byte[] data = encoding.GetBytes(postData);

HttpWebRequest myRequest = (HttpWebRequest)
WebRequest.Create"http://localhost/index.asp");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
newStream.Write(data,0,data.Length);
newStream.Close();

System.IO.StreamReader st = new StreamReader
(((HttpWebResponse)myRequest.GetResponse
()).GetResponseStream());
Console.Write(st.ReadLine());




PLEASE HELP ME.... I AM BADLY STUCK <weeping>


mE
 
N

Nicholas Paldino [.NET/C# MVP]

You need to set the credentials property on the HttpWebRequest to an
implementation of the ICredentials interface which will have access to the
resource. You can use the NetworkCredentials class to set a user name and
password.

Hope this helps.
 
G

Guest

Yes; it did worked... thankssss....

but.... Mr MVP; PLEASE PLEASE HELP... me just 'bout to
die... can't fight more :S




How can I request a web page using POST while passing some
variables? Like I want to access

http://www.localhost.com/info.aspx

with POST and with variables info=0 and set=1 ?

I have tried stupid HttpWebRequest BUT it is NOT
WORKING ... I posted the question few days back BUT nobody
answered it... GURU PPL where ARE YOU???

me doing this..




string lcUrl = "http://localhost/test.php";
HttpWebRequest loHttp = (HttpWebRequest) WebRequest.Create
(lcUrl);


string lcPostData = "info=" +
System.Web.HttpUtility.UrlEncode("0") + "&set=" +
System.Web.HttpUtility.UrlEncode("1");

loHttp.Method="POST";

byte [] lbPostBuffer = System.Text.Encoding.GetEncoding
(1252).GetBytes(lcPostData);

loHttp.ContentLength = lbPostBuffer.Length;

Stream loPostData = loHttp.GetRequestStream();
loPostData.Write(lbPostBuffer,0,lbPostBuffer.Length);
loPostData.Flush();
loPostData.Close();

HttpWebResponse loWebResponse = (HttpWebResponse)
loHttp.GetResponse();
System.Text.Encoding enc = System.Text.Encoding.GetEncoding
(1252);
StreamReader loResponseStream = new StreamReader
(loWebResponse.GetResponseStream(),enc);

string lcHtml = loResponseStream.ReadToEnd();
Console.Write(lcHtml);
loWebResponse.Close();
loResponseStream.Close();













-----Original Message-----
You need to set the credentials property on the HttpWebRequest to an
implementation of the ICredentials interface which will have access to the
resource. You can use the NetworkCredentials class to set a user name and
password.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hi;

I am getting "Unhandled Exception:
System.Net.WebException: The remote server returned an erro
r: (401) Unauthorized."


when I am trying to get a page via post. Code follows...




System.Text.ASCIIEncoding encoding = new
System.Text.ASCIIEncoding();
string postData="age=23&state=47";
byte[] data = encoding.GetBytes(postData);

HttpWebRequest myRequest = (HttpWebRequest)
WebRequest.Create"http://localhost/index.asp");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form- urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
newStream.Write(data,0,data.Length);
newStream.Close();

System.IO.StreamReader st = new StreamReader
(((HttpWebResponse)myRequest.GetResponse
()).GetResponseStream());
Console.Write(st.ReadLine());




PLEASE HELP ME.... I AM BADLY STUCK <weeping>


mE


.
 
R

Rick Strahl [MVP]

You need to send a few request headers to let the server know what you're
sending. Specifically you need to tell it the content type and
content-length.

Take a look here:

http://www.west-wind.com/articles.asp

And look at the "Retrieve HTTP Content with .Net". The article comes with
code for a wrapper class that provides post support for several content
types as well as a few other things like authentication and proxy info in a
single interface.

REgards,

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/wwHelp
----------------------------------
Making waves on the Web


Yes; it did worked... thankssss....

but.... Mr MVP; PLEASE PLEASE HELP... me just 'bout to
die... can't fight more :S




How can I request a web page using POST while passing some
variables? Like I want to access

http://www.localhost.com/info.aspx

with POST and with variables info=0 and set=1 ?

I have tried stupid HttpWebRequest BUT it is NOT
WORKING ... I posted the question few days back BUT nobody
answered it... GURU PPL where ARE YOU???

me doing this..




string lcUrl = "http://localhost/test.php";
HttpWebRequest loHttp = (HttpWebRequest) WebRequest.Create
(lcUrl);


string lcPostData = "info=" +
System.Web.HttpUtility.UrlEncode("0") + "&set=" +
System.Web.HttpUtility.UrlEncode("1");

loHttp.Method="POST";

byte [] lbPostBuffer = System.Text.Encoding.GetEncoding
(1252).GetBytes(lcPostData);

loHttp.ContentLength = lbPostBuffer.Length;

Stream loPostData = loHttp.GetRequestStream();
loPostData.Write(lbPostBuffer,0,lbPostBuffer.Length);
loPostData.Flush();
loPostData.Close();

HttpWebResponse loWebResponse = (HttpWebResponse)
loHttp.GetResponse();
System.Text.Encoding enc = System.Text.Encoding.GetEncoding
(1252);
StreamReader loResponseStream = new StreamReader
(loWebResponse.GetResponseStream(),enc);

string lcHtml = loResponseStream.ReadToEnd();
Console.Write(lcHtml);
loWebResponse.Close();
loResponseStream.Close();













-----Original Message-----
You need to set the credentials property on the HttpWebRequest to an
implementation of the ICredentials interface which will have access to the
resource. You can use the NetworkCredentials class to set a user name and
password.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hi;

I am getting "Unhandled Exception:
System.Net.WebException: The remote server returned an erro
r: (401) Unauthorized."


when I am trying to get a page via post. Code follows...




System.Text.ASCIIEncoding encoding = new
System.Text.ASCIIEncoding();
string postData="age=23&state=47";
byte[] data = encoding.GetBytes(postData);

HttpWebRequest myRequest = (HttpWebRequest)
WebRequest.Create"http://localhost/index.asp");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form- urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
newStream.Write(data,0,data.Length);
newStream.Close();

System.IO.StreamReader st = new StreamReader
(((HttpWebResponse)myRequest.GetResponse
()).GetResponseStream());
Console.Write(st.ReadLine());




PLEASE HELP ME.... I AM BADLY STUCK <weeping>


mE


.
 

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