By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,141 Members | 1,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,141 IT Pros & Developers. It's quick & easy.

HttpWebRequest To send POST HTTP request and Read...

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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]
- mv*@spam.guard.caspershouse.com

"ME" <an*******@discussions.microsoft.com> wrote in message
news:0f****************************@phx.gbl...
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

Nov 15 '05 #2

P: n/a
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.Lengt h);
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 animplementation of the ICredentials interface which will have access to theresource. You can use the NetworkCredentials class to set a user name andpassword.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"ME" <an*******@discussions.microsoft.com> wrote in messagenews:0f****************************@phx.gbl...
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

.

Nov 15 '05 #3

P: n/a
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
<an*******@discussions.microsoft.com> wrote in message
news:01****************************@phx.gbl...
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.Lengt h);
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]
- mv*@spam.guard.caspershouse.com

"ME" <an*******@discussions.microsoft.com> wrote in

message
news:0f****************************@phx.gbl...
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

.

Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.