问题描述
HttpHelps类:代码如下:///<summary>///类说明:HttpHelps类,用来实现Http访问,Post或者Get方式的,直接访问,带Cookie的,带证书的等方式///编码日期:2011-08-20///编码人:苏飞///联系方式:361983679Email:sufei.1013@163.comBlogs:http://sufei.cnblogs.com///</summary>usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Net;usingSystem.IO;usingSystem.Text.RegularExpressions;usingSystem.IO.Compression;publicclassHttpHelps{#region预定义方法或者变更//默认的编码publicEncodingencoding=Encoding.Default;//HttpWebRequest对象用来发起请求publicHttpWebRequestrequest=null;//获取影响流的数据对象privateHttpWebResponseresponse=null;publicBooleanisToLower=true;//读取流的对象privateStreamReaderreader=null;//需要返回的数据对象privatestringreturnData="StringError";///<summary>///根据相传入的数据,得到相应页面数据///</summary>///<paramname="strPostdata">传入的数据Post方式,get方式传NUll或者空字符串都可以</param>///<returns>string类型的响应数据</returns>privatestringGetHttpRequestData(stringstrPostdata){try{//支持跳转页面,查询结果将是跳转后的页面request.AllowAutoRedirect=false;//验证在得到结果时是否有传入数据if(!string.IsNullOrEmpty(strPostdata)&&request.Method.Trim().ToLower().Contains("post")){byte[]buffer=encoding.GetBytes(strPostdata);request.ContentLength=buffer.Length;request.GetRequestStream().Write(buffer,0,buffer.Length);}////最大连接数//request.ServicePoint.ConnectionLimit=1024;#region得到请求的responseusing(response=(HttpWebResponse)request.GetResponse()){//从这里开始我们要无视编码了if(encoding==null){MemoryStream_stream=newMemoryStream();if(response.ContentEncoding!=null&&response.ContentEncoding.Equals("gzip",StringComparison.InvariantCultureIgnoreCase)){//开始读取流并设置编码方式//newGZipStream(response.GetResponseStream(),CompressionMode.Decompress).CopyTo(_stream,10240);//.net4.0以下写法_stream=GetMemoryStream(response.GetResponseStream());}else{//response.GetResponseStream().CopyTo(_stream,10240);//.net4.0以下写法_stream=GetMemoryStream(response.GetResponseStream());}byte[]RawResponse=_stream.ToArray();stringtemp=Encoding.Default.GetString(RawResponse,0,RawResponse.Length);//<meta(.*?)charset([s]?)=[^>](.*?)>Matchmeta=Regex.Match(temp,"<meta([^<]*)charset=([^<]*)["']",RegexOptions.IgnoreCase|RegexOptions.Multiline);stringcharter=(meta.Groups.Count>2)?meta.Groups[2].Value:string.Empty;charter=charter.Replace(""",string.Empty).Replace("'",string.Empty).Replace(";",string.Empty);if(charter.Length>0){encoding=Encoding.GetEncoding(charter);}else{if(response.CharacterSet.ToLower().Trim()=="iso-8859-1"){encoding=Encoding.GetEncoding("gbk");}else{if(string.IsNullOrEmpty(response.CharacterSet.Trim())){encoding=Encoding.UTF8;}else{encoding=Encoding.GetEncoding(response.CharacterSet);}}}returnData=encoding.GetString(RawResponse);}else{if(response.ContentEncoding!=null&&response.ContentEncoding.Equals("gzip",StringComparison.InvariantCultureIgnoreCase)){//开始读取流并设置编码方式using(reader=newStreamReader(newGZipStream(response.GetResponseStream(),CompressionMode.Decompress),encoding)){returnData=reader.ReadToEnd();}}else{//开始读取流并设置编码方式using(reader=newStreamReader(response.GetResponseStream(),encoding)){returnData=reader.ReadToEnd();}}}}#endregion}catch(Exception){//这里是在发生异常时返回的错误信息returnData="StringError";}if(isToLower){returnData=returnData.ToLower();}returnreturnData;}///<summary>///4.0以下.net版本取水运///</summary>///<paramname="streamResponse"></param>privatestaticMemoryStreamGetMemoryStream(StreamstreamResponse){MemoryStream_stream=newMemoryStream();intLength=256;Byte[]buffer=newByte[Length];intbytesRead=streamResponse.Read(buffer,0,Length);//writetherequiredbyteswhile(bytesRead>0){_stream.Write(buffer,0,bytesRead);bytesRead=streamResponse.Read(buffer,0,Length);}return_stream;}///<summary>///传入一个正确或不正确的URl,返回正确的URL///</summary>///<paramname="URL">url</param>///<returns></returns>publicstaticstringGetUrl(stringURL){if(!(URL.Contains("http://")||URL.Contains("https://"))){URL="http://"+URL;}returnURL;}一次不能发太多,接下面
解决方案
本帖最后由 ccttt2012 于 2012-03-27 09:30:48 编辑
解决方案二:
///<summary>///为请求准备参数///</summary>///<paramname="_URL">请求的URL地址</param>///<paramname="_Method">请求方式Get或者Post</param>///<paramname="_Accept">Accept</param>///<paramname="_ContentType">ContentType返回类型</param>///<paramname="_UserAgent">UserAgent客户端的访问类型,包括浏览器版本和操作系统信息</param>///<paramname="_Encoding">读取数据时的编码方式</param>privatevoidSetRequest(string_URL,string_Method,string_Accept,string_ContentType,string_UserAgent,Encoding_Encoding){//初始化对像,并设置请求的URL地址request=(HttpWebRequest)WebRequest.Create(GetUrl(_URL));//请求方式Get或者Postrequest.Method=_Method;//Acceptrequest.Accept=_Accept;//ContentType返回类型request.ContentType=_ContentType;//UserAgent客户端的访问类型,包括浏览器版本和操作系统信息request.UserAgent=_UserAgent;//读取数据时的编码方式encoding=_Encoding;}#endregion#region普通类型///<summary>///采用https协议GET|POST方式访问网络,根据传入的URl地址,得到响应的数据字符串。///</summary>///<paramname="_URL"></param>///<paramname="_Method">请求方式Get或者Post</param>///<paramname="_Accept">Accept</param>///<paramname="_ContentType">ContentType返回类型</param>///<paramname="_UserAgent">UserAgent客户端的访问类型,包括浏览器版本和操作系统信息</param>///<paramname="_Encoding">读取数据时的编码方式</param>///<paramname="_Postdata">只有_Method为Post方式时才需要传入值</param>///<returns>返回Html源代码</returns>publicstringGetHttpRequestString(string_URL,string_Method,string_Accept,string_ContentType,string_UserAgent,Encoding_Encoding,string_Postdata){//准备参数SetRequest(_URL,_Method,_Accept,_ContentType,_UserAgent,_Encoding);//调用专门读取数据的类returnGetHttpRequestData(_Postdata);}///<summary>///采用https协议GET方式访问网络,根据传入的URl地址,得到响应的数据字符串。///</summary>///<paramname="URL">url地址</param>///<paramname="objencoding">编码方式例如:System.Text.Encoding.UTF8;</param>///<returns>String类型的数据</returns>publicstringGetHttpRequestStringByNUll_Get(stringURL,Encodingobjencoding){//准备参数SetRequest(URL,"GET","text/html,application/xhtml+xml,*/*","text/html","Mozilla/5.0(compatible;MSIE9.0;WindowsNT6.1;Trident/5.0)",objencoding);//调用专门读取数据的类returnGetHttpRequestData("");}///<summary>///采用https协议GET方式访问网络,根据传入的URl地址,得到响应的数据字符串。///</summary>///<paramname="URL">url地址</param>///<paramname="objencoding">编码方式例如:System.Text.Encoding.UTF8;</param>///<paramname="stgrcookie">Cookie字符串</param>///<returns>String类型的数据</returns>publicstringGetHttpRequestStringByNUll_GetBycookie(stringURL,Encodingobjencoding,stringstgrcookie){//准备参数SetRequest(URL,"GET","text/html,application/xhtml+xml,*/*","text/html","Mozilla/5.0(compatible;MSIE9.0;WindowsNT6.1;Trident/5.0)",objencoding);request.Headers[HttpRequestHeader.Cookie]=stgrcookie;//调用专门读取数据的类returnGetHttpRequestData("");}///<summary>///采用https协议GET方式访问网络,根据传入的URl地址,得到响应的数据字符串。///</summary>///<paramname="URL">url地址</param>///<paramname="objencoding">编码方式例如:System.Text.Encoding.UTF8;</param>///<returns>String类型的数据</returns>publicstringGetHttpRequestStringByNUll_Get(stringURL,Encodingobjencoding,string_Accept,stringuseragent){//准备参数SetRequest(URL,"GET",_Accept,"text/html",useragent,objencoding);//调用专门读取数据的类returnGetHttpRequestData("");}///<summary>///采用https协议Post方式访问网络,根据传入的URl地址,得到响应的数据字符串。///</summary>///<paramname="URL">url地址</param>///<paramname="strPostdata">Post发送的数据</param>///<paramname="objencoding">编码方式例如:System.Text.Encoding.UTF8;</param>///<returns>String类型的数据</returns>publicstringGetHttpRequestStringByNUll_Post(stringURL,stringstrPostdata,Encodingobjencoding){//准备参数SetRequest(URL,"post","text/html,application/xhtml+xml,*/*","text/html","Mozilla/5.0(compatible;MSIE9.0;WindowsNT6.1;Trident/5.0)",objencoding);//调用专门读取数据的类returnGetHttpRequestData(strPostdata);}#endregion}我调用是privatevoidbutton1_Click(objectsender,EventArgse){HttpHelpsmyHttp=newHttpHelps();stringurl="https://accounts.google.com/ServiceLoginAuth";stringpageData=myHttp.GetHttpRequestStringByNUll_Get(url,myHttp.encoding);textBox1.Text=pageData;}不知道错在哪里,总是返回 stringerror
解决方案三:
晕了都.
解决方案四:
代码太长了.不知看哪点
解决方案五:
没太看懂啥问题,up下吧