///<summary>///获得页面的html源码主要用于后台生成静态文件时获得源码///</summary>///<paramname="url"></param>///<returns></returns>publicstaticstringGetPageHTML(stringurl)
{
stringhttpString=string.Empty;
WebRequestrequest=WebRequest.Create(url);
request.Timeout=200000;
using(HttpWebResponseresponse=(HttpWebResponse)request.GetResponse())
{
if(response.StatusDescription.ToLower().Equals("ok"))
{
using(StreamReaderwriter=newStreamReader(response.GetResponseStream(),Encoding.GetEncoding("GB2312")))
{
httpString=writer.ReadToEnd();
}
}
}
returnhttpString;
}
///<summary>///获得页面的html源码主要用于后台生成静态文件时获得源码UTF-8///</summary>///<paramname="url"></param>///<returns></returns>publicstaticstringGetPageHTMLUTF8(stringurl)
{
stringhttpString=string.Empty;
WebRequestrequest=WebRequest.Create(url);
request.Timeout=200000;
using(HttpWebResponseresponse=(HttpWebResponse)request.GetResponse())
{
if(response.StatusDescription.ToLower().Equals("ok"))
{
using(StreamReaderwriter=newStreamReader(response.GetResponseStream(),Encoding.GetEncoding("UTF-8")))
{
httpString=writer.ReadToEnd();
}
}
}
returnhttpString;
}
///<summary>///post提交JSON数据。支持.net4.0及以下的版本///</summary>///<paramname="url"></param>///<paramname="json"></param>///<returns></returns>publicstaticstringGetHtmlByJson(stringurl,stringjson="")
{
varresult=string.Empty;
try{
varrequest=WebRequest.Create(url)asHttpWebRequest;
request.ContentType="text/json";
request.Method="post";
//request.CookieContainer=_cookie;
using(varstreamWriter=newStreamWriter(request.GetRequestStream()))
{
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
varresponse=(HttpWebResponse)request.GetResponse();
using(varreader=newStreamReader(response.GetResponseStream()))
{
result=reader.ReadToEnd();
}
}
}
catch(UriFormatExceptionuex)
{
//出错处理}
returnresult;
}
///<summary>///利用WebClient远程POST数据并返回数据///</summary>///<paramname="strUrl">远程URL地址</param>///<paramname="strParams">参数,要提交的JSON字符串</param>///<paramname="RespEncode">POST数据的编码</param>///<paramname="ReqEncode">获取数据的编码</param>///<returns></returns>publicstaticstringPostData(stringstrUrl,stringstrParams,EncodingRespEncode,EncodingReqEncode)
{
/***本函数只支持.net4.5以上的框架HttpClienthttpclient=newHttpClient();try{//打开页面httpclient.Credentials=CredentialCache.DefaultCredentials;//从指定的URI下载资源byte[]responseData=httpclient.DownloadData(strUrl);stringsrcString=RespEncode.GetString(responseData);
httpclient.Headers.Add("Content-Type","application/x-www-form-urlencoded");stringpostString=strParams;//将字符串转换成字节数组byte[]postData=Encoding.ASCII.GetBytes(postString);//上传数据,返回页面的字节数组responseData=httpclient.UploadData(strUrl,"POST",postData);srcString=ReqEncode.GetString(responseData);
returnsrcString;}catch(Exceptionex){//记录异常日志//释放资源httpclient.Dispose();returnstring.Empty;}*/return"";
}
///<summary>///执行POST提交范例///</summary>///<paramname="url"></param>///<paramname="postdata">"LoginName=365admin&Password=fob123"</param>///<returns></returns>publicstaticstringPostPageHTMLUTF8(stringurl,stringpostdata)
{
WebClientclient=newWebClient();
System.Collections.Specialized.NameValueCollectionlist=newSystem.Collections.Specialized.NameValueCollection();
list.Add("opencheckindatatype","3");
list.Add("starttime","1492617600");
list.Add("endtime","1492790400");
//"useridlist":["james","paul"]list.Add("useridlist","['TuHuaXing']");
byte[]j=client.UploadValues(url,list);
//vardec=BitConverter.ToInt64(j,0);//stringjS=BitConverter.ToString(j);//jS=Convert.ToString(jS,10);returnSystem.Text.Encoding.Default.GetString(j);
//目标页面获取值request.form["id"]
/**WebRequestrequest7=WebRequest.Create(url);request7.Method="POST";
//post传参数byte[]bytes=Encoding.ASCII.GetBytes(postdata);request7.ContentType="application/x-www-form-urlencoded";request7.ContentLength=postdata.Length;request7.SStreamsendStream=request7.GetRequestStream();sendStream.Write(bytes,0,bytes.Length);sendStream.Close();
//得到返回值WebResponseresponse7=request7.GetResponse();stringOrderQuantity=newStreamReader(response7.GetResponseStream(),Encoding.GetEncoding("utf-8")).ReadToEnd();returnOrderQuantity;//转化成json对象处理//List<GetOrderQuantity>getOrderQuantity=sr.Deserialize<List<GetOrderQuantity>>(OrderQuantity);**/}
本文转载自中文网 |