C # uses HttpWebRequest post to submit data with examples of cookies and related parameters

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Configuration;usingSystem.Data.SqlClient;usingSystem.Data;usingSystem.Diagnostics;usingSystem.Net;usingSystem.IO;namespacesample2{classProgram {Static voidMain (string[] args) {            Try            {                //Data                stringCOOKIESTR ="51fd9f14fa7561b5"; stringPostData =string. Format ("Userid={0}&password={1}","Guest","123456"); byte[] data =Encoding.UTF8.GetBytes (postdata); //Prepare Web Request ...HttpWebRequest request = (HttpWebRequest) webrequest.create ("https://www.xxx.com"); Request. Method="POST"; //request. Referer = "https://www.xxx.com";Request. ContentType ="application/x-www-form-urlencoded; Charset=utf-8"; Request. UserAgent="mozilla/5.0 (Windows NT 10.0; Win64; x64) applewebkit/537.36 (khtml, like Gecko) chrome/56.0.2924.87 safari/537.36"; //request. Host = "www.xxx.com";Request. Headers.add ("Cookies", cookiestr); Request. ContentLength=data.                Length; Stream Newstream=request.                GetRequestStream (); //Send the data.Newstream.write (data,0, data.                Length);                Newstream.close (); //Get ResponseHttpWebResponse Myresponse =(HttpWebResponse) request.                GetResponse (); StreamReader Reader=NewStreamReader (Myresponse.getresponsestream (), Encoding.UTF8); stringContent =Reader.                ReadToEnd ();                Console.WriteLine (content);            Console.ReadLine (); }            Catch(Exception) {Throw; }        }    }}
/// <summary>///Call SMS interface to send SMS, need to cooperate with the template/// </summary>/// <param name= "UserName" >Interface User name</param>/// <param name= "Userpwd" >Interface Password</param>/// <param name= "mobile" >send a mobile phone number</param>/// <param name= "Content" >Send content</param>/// <returns>The return value is greater than 0, the sending succeeds, the system-generated task ID or the custom task ID</returns> Public Static stringPostsms (stringUserName,stringUserpwd,stringMobilestringcontent) {    stringSrcstring ="-999"; Try    {        stringPoststring =string. Format ("Username={0}&password={1}&mobile={2}&content={3}", UserName, MD5 (UserName +MD5 (USERPWD)), mobile, content); byte[] PostData = Encoding.UTF8.GetBytes (poststring);//coding, especially Chinese characters, in advance to see how to crawl the page encoding        stringURL ="http://xxx/smsSend.do";//AddressWebClient WebClient =NewWebClient (); WEBCLIENT.HEADERS.ADD ("Content-type","application/x-www-form-urlencoded");//To take the post must be added to the header, if you change to get a way to remove this sentence can be        byte[] responsedata = Webclient.uploaddata (URL,"POST", PostData);//get a stream of returned characterssrcstring = Encoding.UTF8.GetString (responsedata);//decoding    }    Catch { }    returnsrcstring;}
    classProgram {Static voidMain (string[] args) {            varstr = DownloadStringAsync (NewUri ("http://www.baidu.com")); Console.WriteLine (str.            Result);        Console.ReadLine (); }        Static Asynctask<string>DownloadStringAsync (Uri uri) {//WebClient does not support timeout settings//while Httpwebrequst allows you to set the request header or require more control over the content            varWebClient =NewWebClient (); Webclient.encoding=System.Text.Encoding.UTF8; varresult =awaitWebclient.downloadstringtaskasync (URI); returnresult; }    }

C # uses HttpWebRequest post to submit data with examples of cookies and related parameters

Related Article

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.