HttpRequest Get and Post call methods on other pages
//Get Request way private string RequestGet(string Url) { string PageStr = string.Empty;// Used for storage and return html Uri url = new Uri(Url);//Uri class Provide system 1 Resource identifier (URI) Object representation and pair URI Easy access to all parts. Is the handling url address try { HttpWebRequest httprequest = (HttpWebRequest)WebRequest.Create(url);// According to the url Address to create HTTpWebRequest object #region Parameter Settings httprequest.Method = "get"; //--------------------------------------------- set 1 Some parameters (not necessary) //httprequest.KeepAlive = false;// The persistent connection is set to false //httprequest.ProtocolVersion = HttpVersion.Version11;// The version of the network protocol //httprequest.Proxy = WebProxy.GetDefaultProxy();// Server agent //httprequest.ContentType = "application/x-www-form-urlencoded";//http head //httprequest.AllowAutoRedirect = true; //httprequest.MaximumAutomaticRedirections = 10; //httprequest.Timeout = 30000;// Set the timeout 10 Seconds (milliseconds) //httprequest.UserAgent = "mozilla/4.0 (compatible; msie 6.0; windows nt 5.1)"; // The browser //================================================= #endregion HttpWebResponse response = (HttpWebResponse)httprequest.GetResponse();// use HttpWebResponse Gets the return value of the request Stream steam = response.GetResponseStream();// Gets the data flow from the return object StreamReader reader = new StreamReader(steam, Encoding.GetEncoding("gb2312"));// Read the data Encoding.GetEncoding("gb2312") Refers to the code is gb2312 , don't let the Chinese will be garbled PageStr = reader.ReadToEnd(); reader.Close(); } catch (Exception e) { PageStr += e.Message; } return PageStr; }
//Post Request mode, yu Get Is written in a similar way, so there is less explanation 1 O 'clock private string RequestPost(string Url,string Context)// The two parameters are Url The address and Post Past data { 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; }
public string WebClientGet(string url) { var client = new WebClient(); client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"); var stream = client.OpenRead(url); if (stream == null) return ""; var reader = new StreamReader(stream, Encoding.Default); var result = reader.ReadToEnd(); stream.Close(); reader.Close(); return result; }