//通过Posting发送的数据
string payload="chkbook=buku&kata kunci=管理";
Persyaratan WebRequest = WebRequest.Create(" http://localhost/pceo/Search.aspx ");
req.Metode = "POSTING";
req.ContentType = "application/x-www-form-urlencoded";
StringBuilder UrlEncoded = StringBuilder baru();
Char[] dicadangkan = {'?', '=', '&'};
byte[] SomeBytes = nol;
jika (muatan != nol)
{
ke dalam i=0, j;
while(i<muatan.Panjangnya)
{
j=payload.IndexOfAny(dipesan, i);
jika (j==-1)
{
UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, payload.Length-i),System.Text .Encoding .GetEncoding ("gb2312")));
merusak;
}
UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, ji),System.Text .Encoding .GetEncoding ("gb2312")));
UrlEncoded.Append(payload.Substring(j,1));
saya = j+1;
}
SomeBytes = Pengkodean.Default.GetBytes(UrlEncoded.ToString());
req.ContentLength = SomeBytes.Length;
Aliran newStream = req.GetRequestStream();
newStream.Write(SomeBytes, 0, SomeBytes.Length);
aliran baru.Tutup();
}
kalau tidak
{
req.ContentLength = 0;
}
mencoba
{
Hasil WebResponse = req.GetResponse();
Aliran AcceptStream = hasil.GetResponseStream();
Byte[] baca = Byte baru[512];
int byte = MenerimaStream.Baca(baca, 0, 512);
txtHTML.InnerHtml = "";
sementara (byte > 0)
{
// 注意:
// 下面假定响应使用 UTF-8 作为编码方式。
// 如果内容以 ANSI 代码页形式(例如,932)发送,则使用类似下面的语句:
// Pengkodean encode = System.Text.Encoding.GetEncoding("shift-jis");
Pengkodean enkode = System.Text.Encoding.GetEncoding("gb2312");
txtHTML.InnerHtml = txtHTML.InnerHtml + encode.GetString(baca, 0, byte);
byte = MenerimaStream.Baca(baca, 0, 512);
}
}
menangkap (Pengecualian)
{
txtHTML.InnerHtml = "检索页时出错";
}
出处:狂风之家 BLOG