您好,登錄后才能下訂單哦!
這篇文章主要介紹了post提交獲得html頁面的方法的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇post提交獲得html頁面的方法文章都會有所收獲,下面我們一起來看看吧。
///<summary>///獲得頁面的html源碼主要用于后臺生成靜態(tài)文件時獲得源碼///</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源碼主要用于后臺生成靜態(tài)文件時獲得源碼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;//將字符串轉換成字節(jié)數組byte[]postData=Encoding.ASCII.GetBytes(postString);//上傳數據,返回頁面的字節(jié)數組responseData=httpclient.UploadData(strUrl,"POST",postData);srcString=ReqEncode.GetString(responseData);
returnsrcString;}catch(Exceptionex){//記錄異常日志//釋放資源httpclient.Dispose();returnstring.Empty;}*/return"";
}
///<summary>///執(zhí)行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);**/}
關于“post提交獲得html頁面的方法”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“post提交獲得html頁面的方法”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。