//透過Post傳送的數據
string payload="chkbook=book&keyword=管理";
WebRequest req = WebRequest.Create(" http://localhost/pceo/Search.aspx ");
請求方法 = "POST";
req.ContentType = "application/x-www-form-urlencoded";
StringBuilder UrlEncoded = new StringBuilder();
Char[] 保留 = {'?', '=', '&'};
byte[] SomeBytes = null;
if(有效負載!= null)
{
int i = 0,j;
while(i<有效負載.長度)
{
j=payload.IndexOfAny(保留, i);
如果(j==-1)
{
UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i,payload.Length-i),System.Text.Encoding.GetEncoding(「gb2312」)));
休息;
}
UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, ji),System.Text .Encoding .GetEncoding("gb2312")));
UrlEncoded.Append(payload.Substring(j,1));
我 = j+1;
}
SomeBytes = Encoding.Default.GetBytes(UrlEncoded.ToString());
req.ContentLength = SomeBytes.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(SomeBytes, 0, SomeBytes.Length);
newStream.Close();
}
別的
{
請求內容長度 = 0;
}
嘗試
{
WebResponse 結果 = req.GetResponse();
流 ReceiveStream = result.GetResponseStream();
字節[]讀取=新字節[512];
int 位元組 = ReceiveStream.Read(讀, 0, 512);
txtHTML.InnerHtml = "";
而(位元組> 0)
{
// 注意:
// 以下假設回應使用UTF-8作為編碼方式。
// 如果內容以ANSI代碼頁形式(例如,932)傳送,則使用類似下面的語句:
// 編碼encode = System.Text.Encoding.GetEncoding("shift-jis");
編碼 編碼 = System.Text.Encoding.GetEncoding("gb2312");
txtHTML.InnerHtml = txtHTML.InnerHtml + 編碼.GetString(read, 0, bytes);
位元組 = ReceiveStream.Read(讀, 0, 512);
}
}
捕獲(異常)
{
txtHTML.InnerHtml = "檢索頁時發生錯誤";
}
出處:狂風之家BLOG