这篇文章主要介绍了post提交获得html页面的方法的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇post提交获得html页面的方法文章都会有所收获,下面我们一起来看看吧。
///<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);**/}
关于“post提交获得html页面的方法”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“post提交获得html页面的方法”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。