使用Post进行WebRequest登录 [英] WebRequest login using Post
问题描述
所以我试图学习如何使用HttpWebRequst访问网页,并且试图将登录凭据发送到该站点.
So i'm trying to learn how to access web pages using HttpWebRequst, and I'm trying to send login credentials to the site.
我在Google上四处搜寻,发现了一些有关使用request.Method ="post";但是我从那儿什么都没有.
I googled around and found something about using request.Method="post"; but i got no where from there.
我知道它与网站源代码中的这一部分有关:
I know it has soemthing to do with this part in the web site source code:
< form action ="/" method ="post">
<标签>< span> Benutzername:</span><输入类=文本"名称="b" /></label>
<标签><跨度>密码:</span><输入类别=文本" type ="password"名称="p" /></label>
<输入类型=隐藏的".名称="r"值=" />
< input class =提交"; type ="submit" value ="Anmelden"; />
</form>
<form action="/" method="post">
<label><span>Benutzername:</span><input class="text" name="b" /></label>
<label><span>Passwort:</span><input class="text" type="password" name="p" /></label>
<input type="hidden" name="r" value="" />
<input class="submit" type="submit" value="Anmelden" />
</form>
这是我尝试的代码:
user = use.Text;
passs = pass.Text;
string useVal;
string pasVal;
Encoding encode = Encoding.ASCII;
useVal = "&class=text&name=" + user;
useVal += "&class=text&type=password&name=" + passs;
useVal += "&type=hidden&name=r&value=";
byte[] data = encode.GetBytes(useVal);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://endoftheinter.net/");
req.Credentials = new NetworkCredential(user, passs);
req.Method = "post";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = data.Length;
Stream stre = req.GetRequestStream();
stre.Write(data, 0, data.Length);
stre.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
CookieCollection cookie = res.Cookies;
Stream recStream = res.GetResponseStream();
StreamReader read = new StreamReader(recStream, encode);
string strResponse = read.ReadToEnd();
txt.Text = strResponse;
read.Close();
res.Close();
推荐答案
您是否检查过是否确实发布了网站所需的所有参数?
Did you check that you realy POSTed all the parameters that the website needs?
要获取该信息,请使用Microsoft的提琴手:
In order to have that information use Microsoft's fiddler:
http://www.fiddler2.com/Fiddler2/version.asp
使用此Web调试器,您将能够查看发送到服务器的所有部件.
With this web debugger you'll be able to see all the parts that were sent to the server.
Noam B
_________________________________________________________
请不要忘记回答/帮助投票.鼓励我们为您提供帮助...
Noam B
_________________________________________________________
Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
这篇关于使用Post进行WebRequest登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!