[C#] 纯文本查看 复制代码
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/think/index.php/Index/login");
request.CookieContainer = new CookieContainer();
CookieContainer cookie = request.CookieContainer;//如果用不到Cookie,删去即可
//以下是发送的http头,随便加,其中referer挺重要的,有些网站会根据这个来反盗链
request.Referer = "http://localhost/";
request.Accept = "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.Headers["Accept-Language"] = "zh-CN,zh;q=0.";
request.Headers["Accept-Charset"] = "GBK,utf-8;q=0.7,*;q=0.3";
request.UserAgent = "User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1";
request.KeepAlive = true;
//上面的http头看情况而定,但是下面俩必须加
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
string postString = "password=111111";
Encoding encoding = Encoding.UTF8;//根据网站的编码自定义
byte[] postData = encoding.GetBytes(postString);//postDataStr即为发送的数据,格式还是和上次说的一样
request.ContentLength = postData.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(postData, 0, postData.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
//如果http头中接受gzip的话,这里就要判断是否为有压缩,有的话,直接解压缩即可
if (response.Headers["Content-Encoding"] != null && response.Headers["Content-Encoding"].ToLower().Contains("gzip"))
{
responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
}
StreamReader streamReader = new StreamReader(responseStream, encoding);
string retString = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
System.Diagnostics.Trace.WriteLine(retString);
自己参考网上的写了一个,经过测试,没有问题。
基本可以判断那个C#HttpHelper类2.0框架版本,是存在问题的。
|