110,534
社区成员
发帖
与我相关
我的任务
分享
WinHttp winhttp = new WinHttp();
string content = winhttp.Open("GET", "http://su.58.com/", false);
winhttp.Post();
string Hash = winhttp.ResponseBody;
public string Open(string OpenMethod, string URL, bool Async)
{
object obj;
obj = HttpObj.DoMethod("Open", new object[] { OpenMethod, URL, Async });
if (obj != null)
{
_Active = false;
return obj.ToString();
}
else
{
SetRequestHeader("Content-Type", _ContentType);
SetRequestHeader("User-Agent", "Mozilla/5.0");
SetRequestHeader("Cache-Control", "max-age=0");
SetRequestHeader("Accept", "*/*");
SetRequestHeader("Accept-Encoding", "gzip,deflate,sdch");
SetRequestHeader("Accept-Language", "zh-CN,zh;q=0.8");
if (_ContentLength != 0) SetRequestHeader("Content-Length", _ContentLength);
_Active = true;
return "True";
}
}
public string ResponseBody
{
get
{
if (_Active)
{
ComObject AdoStream = new ComObject("Adodb.Stream");
AdoStream["Type"] = 1;
AdoStream["Mode"] = 3;
AdoStream.DoMethod("Open", new object[] { });
AdoStream.DoMethod("Write", new object[1] { HttpObj["ResponseBody"] });
AdoStream["Position"] = 0;
AdoStream["Type"] = 2;
AdoStream["Charset"] = "UTF-8";//这里的编码和网页上的一样
// GZipStream g = new GZipStream((Stream)AdoStream, CompressionMode.Decompress);
// string content = g
return AdoStream["ReadText"].ToString();
}
else return "";
}
}
private string Html(string Url)
{
string strResult = "";
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string ce = response.ContentEncoding;
Stream streamReceive = response.GetResponseStream();
Encoding encoding = Encoding.GetEncoding(GetChartset(Url));
if (ce.ToLower() == "gzip")
{
GZipStream gzip = new GZipStream(streamReceive, CompressionMode.Decompress);
using (StreamReader reader = new StreamReader(gzip,encoding))
{
strResult = reader.ReadToEnd();
}
}
}
catch (Exception ex) { HttpContext.Current.Response.Write(ex.ToString()); }
return strResult;
}
public static string doGet(string Url)
{
try
{
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(Url.ToString());
myRequest.ServicePoint.Expect100Continue = true;
myRequest.Method = "GET";
myRequest.Timeout = 6000;
myRequest.KeepAlive = true;//modify by yang
myRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)";
myRequest.ContentType = "application/x-www-form-urlencoded";
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.GetEncoding("utf-8"));
string outdata = reader.ReadToEnd();
reader.Close();
return outdata;
}
catch (Exception ex)
{
return ex.Message;
}
}
try
也可以使用默认的
SetRequestHeader("Accept-Encoding", "gzip,deflate,sdch");这个不要试试