时间:2022-10-16 11:22:57 | 栏目:.NET代码 | 点击:次
//---------------------------------------------设定一些参数(不必要可以)
//httprequest.KeepAlive = false;//持久连接设置为false
//httprequest.ProtocolVersion = HttpVersion.Version11;// 网络协议的版本
//httprequest.Proxy = WebProxy.GetDefaultProxy();//服务器代理
//httprequest.ContentType = "application/x-www-form-urlencoded";//http 头
//httprequest.AllowAutoRedirect = true;
//httprequest.MaximumAutomaticRedirections = 10;
//httprequest.Timeout = 30000;//设定超时十秒(毫秒)
//httprequest.UserAgent = "mozilla/4.0 (compatible; msie 6.0; windows nt 5.1)"; //浏览器
//=================================================
#endregion
HttpWebResponse response = (HttpWebResponse)httprequest.GetResponse();//使用HttpWebResponse获取请求的还回值
Stream steam = response.GetResponseStream();//从还回对象中获取数据流
StreamReader reader = new StreamReader(steam, Encoding.GetEncoding("gb2312"));//读取数据Encoding.GetEncoding("gb2312")指编码是gb2312,不让中文会乱码的
PageStr = reader.ReadToEnd();
reader.Close();
}
catch (Exception e)
{
PageStr += e.Message;
}
return PageStr;
}
private string RequestPost(string Url,string Context)//两个参数分别是Url地址和Post过去的数据
{
string PageStr=string.Empty;
Uri url = new Uri(Url);
byte[] reqbytes=Encoding.ASCII.GetBytes(Context);
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "post";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = reqbytes.Length;
Stream stm = req.GetRequestStream();
stm.Write(reqbytes, 0, reqbytes.Length);
stm.Close();
HttpWebResponse wr = (HttpWebResponse)req.GetResponse();
Stream stream = wr.GetResponseStream();
StreamReader srd= new StreamReader(stream,Encoding.GetEncoding("gb2312"));
PageStr += srd.ReadToEnd();
stream.Close();
srd.Close();
}
catch (Exception e)
{
PageStr += e.Message;
}
return PageStr;
}