//---------------------------------------------設(shè)定一些參數(shù)(不必要可以)
//httprequest.KeepAlive = false;//持久連接設(shè)置為false
//httprequest.ProtocolVersion = HttpVersion.Version11;// 網(wǎng)絡(luò)協(xié)議的版本
//httprequest.Proxy = WebProxy.GetDefaultProxy();//服務(wù)器代理
//httprequest.ContentType = "application/x-www-form-urlencoded";//http 頭
//httprequest.AllowAutoRedirect = true;
//httprequest.MaximumAutomaticRedirections = 10;
//httprequest.Timeout = 30000;//設(shè)定超時(shí)十秒(毫秒)
//httprequest.UserAgent = "mozilla/4.0 (compatible; msie 6.0; windows nt 5.1)"; //瀏覽器
//=================================================
#endregion
HttpWebResponse response = (HttpWebResponse)httprequest.GetResponse();//使用HttpWebResponse獲取請(qǐng)求的還回值
Stream steam = response.GetResponseStream();//從還回對(duì)象中獲取數(shù)據(jù)流
StreamReader reader = new StreamReader(steam, Encoding.GetEncoding("gb2312"));//讀取數(shù)據(jù)Encoding.GetEncoding("gb2312")指編碼是gb2312,不讓中文會(huì)亂碼的
PageStr = reader.ReadToEnd();
reader.Close();
}
catch (Exception e)
{
PageStr += e.Message;
}
return PageStr;
}
private string RequestPost(string Url,string Context)//兩個(gè)參數(shù)分別是Url地址和Post過(guò)去的數(shù)據(jù)
{
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;
}
標(biāo)簽:臺(tái)州 洛陽(yáng) 周口 朔州 百色 朝陽(yáng) 新鄉(xiāng) 喀什
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《HttpRequest Get和Post調(diào)用其他頁(yè)面的方法》,本文關(guān)鍵詞 HttpRequest,Get,和,Post,調(diào)用,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。