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

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

通過(guò)HttpWebRequest 發(fā)送 POST 請(qǐng)求實(shí)現(xiàn)自動(dòng)登陸

2019-11-17 04:27:10
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

怎樣通過(guò)HttpWebRequest 發(fā)送 POST 請(qǐng)求到一個(gè)網(wǎng)頁(yè)服務(wù)器?例如編寫(xiě)個(gè)程序?qū)崿F(xiàn)自動(dòng)用戶登錄,自動(dòng)提交表單數(shù)據(jù)到網(wǎng)站等。
假如某個(gè)頁(yè)面有個(gè)如下的表單(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>從表單可看到表單有兩個(gè)表單域,一個(gè)是userid另一個(gè)是password,所以以POST形式提交的數(shù)據(jù)應(yīng)該包含有這兩項(xiàng)。
其中POST的數(shù)據(jù)格式為:
表單域名稱1=值1&表單域名稱2=值2&表單域名稱3=值3……
要注意的是“值”必須是經(jīng)過(guò)HTMLEncode的,即不能包含“<>=&”這些符號(hào)。

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

用C#寫(xiě)提交程序: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ā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 高平市| 荣昌县| 西安市| 亳州市| 利津县| 鹰潭市| 乌恰县| 常州市| 九龙城区| 阳山县| 施秉县| 屯昌县| 府谷县| 迁西县| 忻州市| 南涧| 抚远县| 陈巴尔虎旗| 兴城市| 大石桥市| 克拉玛依市| 苍溪县| 济阳县| 大理市| 瑞金市| 内黄县| 丰宁| 扬州市| 偃师市| 乌拉特后旗| 固始县| 延庆县| 滦平县| 清流县| 贵港市| 崇仁县| 嘉祥县| 新竹市| 无极县| 山阳县| 普安县|