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
.