//通过Post发送的数据
string payload="chkbook=book&keyword=管理";
WebRequest req = WebRequest.Create(" http://localhost/pceo/Search.aspx ");
req.Method = "POST";
req.ContentType = "aplicativo/x-www-form-urlencoded";
StringBuilder UrlEncoded = new StringBuilder();
Char[] reservado = {'?', '=', '&'};
byte[] SomeBytes = null;
if (carga útil! = nulo)
{
int i=0, j;
enquanto(i<carga.Comprimento)
{
j = payload.IndexOfAny (reservado, i);
se (j==-1)
{
UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, payload.Length-i),System.Text .Encoding .GetEncoding ("gb2312")));
quebrar;
}
UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, ji),System.Text .Encoding .GetEncoding ("gb2312")));
UrlEncoded.Append(carga.Substring(j,1));
eu = j+1;
}
SomeBytes = Encoding.Default.GetBytes(UrlEncoded.ToString());
req.ContentLength = SomeBytes.Length;
Fluxo newStream = req.GetRequestStream();
newStream.Write(SomeBytes, 0, SomeBytes.Length);
newStream.Close();
}
outro
{
req.ContentLength = 0;
}
tentar
{
Resultado WebResponse = req.GetResponse();
Fluxo ReceiveStream = result.GetResponseStream();
Byte[] lido = novo Byte[512];
int bytes = ReceiveStream.Read(ler, 0, 512);
txtHTML.InnerHtml = "";
enquanto (bytes > 0)
{
// 注意:
// 下面假定响应使用 UTF-8 作为编码方式。
// 如果内容以 ANSI 代码页形式(例如,932)发送,则使用类似下面的语句:
// Codificação encode = System.Text.Encoding.GetEncoding("shift-jis");
Codificação codificação = System.Text.Encoding.GetEncoding("gb2312");
txtHTML.InnerHtml = txtHTML.InnerHtml + encode.GetString (leitura, 0, bytes);
bytes = ReceiveStream.Read(ler, 0, 512);
}
}
pegar (exceção)
{
txtHTML.InnerHtml = "检索页时出错";
}
出处:狂风之家 BLOG