Get the App
SLTechnology News&Howtos  ›  Development  › 

The method of getting html pages submitted by post

Shulou Source: shulou.com Published: 2022-06-01 03:25:17 09月16日 Update

This article mainly introduces the relevant knowledge of the method of post submission to obtain html page, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe that you will have something to gain after reading this post submission to get the html page. Let's take a look at it.

/ the html source code of the page is mainly used to obtain the source code / publicstaticstringGetPageHTML (stringurl) when generating static files in the background.

{

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

}

/ the html source code of the page is mainly used to obtain the source code UTF-8/publicstaticstringGetPageHTMLUTF8 (stringurl) when generating static files in the background.

{

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

}

/ post submits JSON data. Supports .net4.0 and below / 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)

{

/ / error handling}

Returnresult

}

/ utilize WebClient remote POST data and return data / remote URL address / / parameter, JSON string / POST data encoding / get the data encoding / publicstaticstringPostData (stringstrUrl,stringstrParams,EncodingRespEncode,EncodingReqEncode)

{

/ * this function only supports framework HttpClienthttpclient=newHttpClient () above .net4.5; try {/ / Open the page httpclient.Credentials=CredentialCache.DefaultCredentials;// to download the resource byte [] responseData=httpclient.DownloadData (strUrl) from the specified URI; stringsrcString=RespEncode.GetString (responseData)

Httpclient.Headers.Add ("Content-Type", "application/x-www-form-urlencoded"); stringpostString=strParams;// converts a string into a byte array byte [] postData=Encoding.ASCII.GetBytes (postString); / / uploads data and returns the byte array responseData=httpclient.UploadData (strUrl, "POST", postData) of the page; srcString=ReqEncode.GetString (responseData)

ReturnsrcString;} catch (Exceptionex) {/ / record exception log / / release resource httpclient.Dispose (); returnstring.Empty;} * / return ""

}

/ execute POST submission example / "LoginName=365admin&Password=fob123" / 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 (jjj0); / / stringjS=BitConverter.ToString (j); / / jS=Convert.ToString (jS,10); returnSystem.Text.Encoding.Default.GetString (j)

/ / get the value request.form on the target page ["id"]

/ * * WebRequestrequest7=WebRequest.Create (url); request7.Method= "POST"

/ / post pass parameters 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 ()

/ / get the return value WebResponseresponse7=request7.GetResponse (); stringOrderQuantity=newStreamReader (response7.GetResponseStream (), Encoding.GetEncoding ("utf-8")) .ReadToEnd (); returnOrderQuantity;// is converted to json object processing / / ListgetOrderQuantity=sr.Deserialize (OrderQuantity); * * /}

This is the end of the article on "how to get html pages submitted by post". Thank you for reading! I believe you all have a certain understanding of the knowledge of "post submit how to obtain html pages". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Pages data methods source code knowledge content parameters background characters strings bytes arrays files articles coding resources static processing support generation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Huawei Docker MySQL Shulou Information