HttpRequest Get Post,WebClient Get
#region HttpRequestGet public string HttpRequestGet(string url) { return HttpRequestGet(url, WebRequestMethods.Http.Get, ""); } public string HttpRequestPost(string url, string data) { return HttpRequestGet(url, WebRequestMethods.Http.Post, data); } private string HttpRequestGet(string url, string method, string data) { var request = WebRequest.Create(url); request.Method = method; if (method == WebRequestMethods.Http.Post) { byte[] buffer = Encoding.Default.GetBytes(data); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = buffer.Length; var streamRequest = request.GetRequestStream(); streamRequest.Write(buffer, 0, buffer.Length); streamRequest.Close(); } var response = request.GetResponse(); var streamResponse = response.GetResponseStream(); if (streamResponse == null) return ""; var reader = new StreamReader(streamResponse, Encoding.Default); var result = reader.ReadToEnd(); streamResponse.Close(); reader.Close(); return result; } #endregion 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; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。