using System.IO;
using System.Text;
using System.Collections.Specialized;
如果表单中要发送中文,可以对数据进行编码gb2312/gbk。
Encoding myencode=Encoding.GetEncoding("gb2312");
然后处理要传的表单数据。
string strpost=HttpUtility.UrlEncode("name",myencode)+"="+HttpUtility.UrlEncode(name,myencode);
有多个参数可以用"&"拼接。
接着序列化参数。
byte[] postBytes=Encoding.ASCII.GetBytes(strpost);
创建请求示例。
HttpWebRequest req=(HttpWebRequest)HttpWebRequest.Create("表单提交到的url");
下面可以选择请求的方式,标头。
req.Method="POST";
req.ContentType="application/x-www-form-urlencoded;charset=gb2312";
req.ContentLength=postBytes.Length;
using(Stream sendStream=req.GetRequestStream())
{
sendStream.Write(postBytes,0,req.ContentLength.ToInt32());
}
这样运行的时候程序就会模拟提交表单了。