//通过Post发送的数据
string payload="chkbook=book&keyword=管理";
WebRequest req = WebRequest.Create(" http://localhost/pceo/Search.aspx ");
req.Method = "POST" ;
req.ContentType = "application/x-www-form-urlencoded";
StringBuilder UrlEncoded = new StringBuilder();
Char[] réservé = {'?', '=', '&'};
octet[] Quelques octets = null;
si (charge utile ! = null)
{
int je=0, j;
while(i<payload.Length)
{
j=payload.IndexOfAny(réservé, i);
si (j==-1)
{
UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, payload.Length-i),System.Text .Encoding .GetEncoding ("gb2312")));
casser;
}
UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, ji),System.Text .Encoding .GetEncoding ("gb2312")));
UrlEncoded.Append(payload.Substring(j,1));
je = j+1 ;
}
SomeBytes = Encoding.Default.GetBytes(UrlEncoded.ToString());
req.ContentLength = SomeBytes.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(Quelques octets, 0, Quelques octets.Longueur);
newStream.Close();
}
autre
{
req.ContentLength = 0 ;
}
essayer
{
Résultat WebResponse = req.GetResponse();
Stream ReceiverStream = result.GetResponseStream();
Octet[] read = nouvel octet[512];
int octets = ReceiverStream.Read(read, 0, 512);
txtHTML.InnerHtml = "";
while (octets > 0)
{
// Remarque :
// Fichiers UTF-8 pour les fichiers UTF-8.
// 如果内容以 ANSI 代码页形式(例如,932)发送,则使用类似下面的语句 :
// Encodage encode = System.Text.Encoding.GetEncoding("shift-jis");
Encodage encode = System.Text.Encoding.GetEncoding("gb2312");
txtHTML.InnerHtml = txtHTML.InnerHtml + encode.GetString(read, 0, bytes);
octets = ReceiverStream.Read (lecture, 0, 512);
}
}
attraper (Exception)
{
txtHTML.InnerHtml = "检索页时出错";
}
出处:狂风之家 BLOG