国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

通過HttpWebRequest 發(fā)送 POST 請求實現(xiàn)自動登陸

2024-07-10 13:13:16
字體:
供稿:網(wǎng)友

怎樣通過HttpWebRequest 發(fā)送 POST 請求到一個網(wǎng)頁服務器?例如編寫個程序?qū)崿F(xiàn)自動用戶登錄,自動提交表單數(shù)據(jù)到網(wǎng)站等。
假如某個頁面有個如下的表單(Form):view plaincopy to clipboardprint?
<form name="form1" action="http:www.breakn.com/login.asp" method="post">  
<input type="text" name="userid" value="">  
<input type="password" name="password" value="">  
</form> 

<form name="form1" action="http:www.breakn.com/login.asp" method="post">
<input type="text" name="userid" value="">
<input type="password" name="password" value="">
</form>從表單可看到表單有兩個表單域,一個是userid另一個是password,所以以POST形式提交的數(shù)據(jù)應該包含有這兩項。
其中POST的數(shù)據(jù)格式為:
表單域名稱1=值1&表單域名稱2=值2&表單域名稱3=值3……
要注意的是“值”必須是經(jīng)過HTMLEncode的,即不能包含“<>=&”這些符號。

本例子要提交的數(shù)據(jù)應該是:
userid=value1&password=value2

用C#寫提交程序:view plaincopy to clipboardprint?
string strId = "guest";  
string strPassword= "123456";  
 
ASCIIEncoding encoding=new ASCIIEncoding();  
string postData="userid="+strId;  
postData += ("&password="+strPassword);  
 
byte[] data = encoding.GetBytes(postData);  
 
// Prepare web request...  
HttpWebRequest myRequest =  
(HttpWebRequest)WebRequest.Create("http:www.here.com/login.asp");  
 
myRequest.Method = "POST";  
myRequest.ContentType="application/x-www-form-urlencoded";  
myRequest.ContentLength = data.Length;  
Stream newStream=myRequest.GetRequestStream();  
 
// Send the data.  
newStream.Write(data,0,data.Length);  
newStream.Close();  
 
// Get response  
HttpWebResponse myResponse=(HttpWebResponse)myRequest.GetResponse();  
StreamReader reader = new StreamReader(response.GetResponseStream(),Encoding.Default);  
string content = reader.ReadToEnd();  
Console.WriteLine(content);  

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 昭通市| 巫溪县| 惠来县| 建平县| 建湖县| 东兰县| 逊克县| 襄樊市| 西和县| 姜堰市| 财经| 邯郸县| 海城市| 象山县| 五大连池市| 科技| 汝城县| 香港 | 蒙山县| 图片| 慈溪市| 梧州市| 昌黎县| 六枝特区| 宜阳县| 清丰县| 合山市| 卫辉市| 同江市| 紫金县| 韶关市| 东明县| 陵水| 西安市| 廉江市| 普洱| 杂多县| 读书| 额敏县| 普洱| 昆明市|