C # request web through get and post

Source: Internet
Author: User

Private string GetModel (string strurl)
{
String strret = NULL;
Try
{
Httpwebrequest request = (httpwebrequest) webrequest. Create (strurl );
Request. Timeout = 2000;
Httpwebresponse response = (httpwebresponse) request. getresponse ();

System. Io. Stream resstream = response. getresponsestream ();
Encoding encode = system. Text. encoding. default;
Streamreader readstream = new streamreader (resstream, encode );

Char [] READ = new char [256];
Int COUNT = readstream. Read (read, 0,256 );
While (count> 0)
{
String STR = new string (read, 0, count );
Strret = strret + STR;
Count = readstream. Read (read, 0,256 );
}

Resstream. Close ();
}
Catch (exception E)
{
Strret = "";
}
Return strret;
}

Public static string postmodel (string strurl, string strparm)
{
Encoding encode = system. Text. encoding. default;

Byte [] arrb = encode. getbytes (strparm );
String strbaseurl = NULL;

Httpwebrequest myreq = (httpwebrequest) webrequest. Create (strurl );
Myreq. method = "Post ";
Myreq. contenttype = "application/X-WWW-form-urlencoded ";
Myreq. contentlength = arrb. length;
Stream outstream = myreq. getrequeststream ();
Outstream. Write (arrb, 0, arrb. Length );
Outstream. Close ();
Webresponse myresp = NULL;
Try
{
// Receives the HTTP Response
Myresp = myreq. getresponse ();
}
Catch (exception E)
{
Int II = 0;
}
Stream receivestream = myresp. getresponsestream ();
Streamreader readstream = new streamreader (receivestream, encode );
Char [] READ = new char [256];
Int COUNT = readstream. Read (read, 0,256 );
String STR = NULL;
While (count> 0)
{
STR + = new string (read, 0, count );
Count = readstream. Read (read, 0,256 );
}
Readstream. Close ();
Myresp. Close ();
Return STR;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.