.NET calls someone else's HTTP page, return results, there are Chinese processing, GET and POST methods in the URL.

xiaoxiao2021-03-06  108

A GET method.

Using system.net; using system.text; using system.web; using msxml2; using system.threading;

namespace Engine.Activity.FeeAgency {///

/// Summary description of Class GetHttp. /// public class GetHttp {private GetHttp () {} public static string Get_Http (string a_strUrl) {string strResult Httpwebrequest myreq = (httpwebRequest) WebRequest.create (a_strurl); myreq.method = "get"; try {httpwebresponse httpwresp = (httpwebresponse) MyReq.getResponse ();

Stream myStream = HttpWResp.GetResponseStream (); StreamReader sr = new StreamReader (myStream, Encoding.Default); StringBuilder strBuilder = new StringBuilder (); while (! -1 = sr.Peek ()) {strBuilder.Append (sr.ReadLine ());

Strresult = strbuilder.tostring ();} catch (exception exp) {strresult = "error:" Exp.Message;

Return Strresult;

}

}

Two POST methods can handle Chinese URL

Using system.net; using system.text; using system.web; using msxml2; using system.threading;

Namespace Engine.Activity.FeeAgency {public class postttp {private postttp () {} public static string post_http (string a_strull, string a_strpostdata) {string strresult = "

Try {encoding encoding = encoding.getencoding ("GB2312");

String postdata = a_strpostdata;

Strincel = a_strurl;

Byte [] data = encoding.getbytes (postData);

// Prepare request ...

Httpwebrequest myrequest = (httpwebRequest) WebRequest.create (Strurl);

MyRequest.Method = "post";

MyRequest.contentType = "Application / X-WWW-FORM-URLENCODED";

MyRequest.contentLength = data.length; stream newstream = myRequest.getRequestStream ();

// send data

Newstream.write (Data, 0, Data.Length); newstream.close ();

Try {httpwebresponse httpwresp = (httpwebresponse) MyRequest.getResponse ();

Stream myStream = HttpWResp.GetResponseStream (); StreamReader sr = new StreamReader (myStream, Encoding.Default); StringBuilder strBuilder = new StringBuilder (); while (! -1 = sr.Peek ()) {strBuilder.Append (sr.ReadLine ());

Strresult = strbuilder.tostring ();} catch (exception exp) {strresult = "error:" Exp.Message;

} CatCH (Exception Exp) {strresult = "Error:" Exp.Message;

Return Strresult;

}}}

转载请注明原文地址:https://www.9cbs.com/read-125067.html

New Post(0)