C # vc http post get)

Source: Internet
Author: User

C # vc http post get

Post is used to submit user input in implicit mode. In ASP, request. getform () is used to obtain the value of the input field;
You can use get to submit user input explicitly. When submitting a request, you can view the user input content in the address bar of your browser (you can enter Java search in Google, you can see Java in the address bar after clicking search), and use request on ASP. getquery () to get the value of the input field;

Summary
To properly simulate a form submission using wininet, you need to send a header that indicates the proper content-type. for forms, the proper Content-Type header is: Content-Type: Application/X-WWW-form-urlencoded

Abstract:

To simulate a form through wininet, you need to send a header to indicate the Content-Type. Generally: Content-Type: Application/X-WWW-form-urlencoded

More information
In response cases, the server does not respond appropriately if a content-type is not specified. for example, the Active Server Pages component of IIS 3.0 actually checks this header specifically for 'application/X-WWW-form-urlencoded' before adding form variables to the "request. form "object. this mime/Content-Type indicates that the data of the request is a list of URL-encoded form variables. URL-e Ncoding means that space character (ASCII 32) is encoded as '+', special character such '! 'Encodedin hexadecemal form as '% 21 '.

Learn more:

In many cases, if Content-Type is not specified, the server may return incorrect results. For example, the ASP Component of IIS 3.0 checks whether the header is 'application/X-WWW-form-urlencoded' before adding a variable to the "request. Form" object '. Mime/Content-Type indicates that the requested data is an array variable of URL-encoded. URL-encoding means that the space (ASCII 32) is replaced by '+', and all special characters are replaced by the corresponding hexadecimal format, such '! '->' % 21 '.

Here is a snippet of code that uses the MFC wininet classes to simulate a form POST request:

The following section uses the MFC wininet class to simulate form submission.Code:

Cstring strheaders =
_ T ("Content-Type: Application/X-WWW-form-urlencoded ");
// URL-encoded form variables-
// Name = "John Doe", userid = "hithere", other = "P & Q"
Cstring strformdata = _ T ("name = John + Doe & userid = hithere & Other = P % 26q ");

Cinternetsession session;
Chttpconnection * pconnection =
Session. gethttpconnection (_ T ("servernamehere "));
Chttpfile * pfile =
Pconnection-> openrequest (chttpconnection: http_verb_post,
_ T ("formactionhere "));
Bool result = pfile-> sendrequest (strheaders,
(Lpvoid) (lpctstr) strformdata, strformdata. getlength ());

Use the SDK to implement the above functions without using MFC:
Without MFC, the same code translates to straight sdk cils as follows:

Static tchar HDRs [] =
_ T ("Content-Type: Application/X-WWW-form-urlencoded ");
Static tchar frmdata [] =
_ T ("name = John + Doe & userid = hithere & Other = P % 26q ");
Statuc tchar accept [] =
_ T ("accept :*/*");

// For clarity, error-checking has been removed
Hinternet hsession = internetopen ("myagent ",
Internet_open_type_preconfig, null, null, 0 );
Hinternet hconnect = internetconnect (hsession, _ T ("servernamehere "),
Internet_default_http_port, null, null, internet_service_http, 0, 1 );
Hinternet hrequest = httpopenrequest (hconnect, "Post ",
_ T ("formactionhere"), null, null, accept, 0, 1 );
Httpsendrequest (hrequest, HDRs, strlen (HDRs), frmdata, strlen (frmdata ));
// Close any valid Internet-handles

Use C # To implement:

// Send sxmlmessage to the specified dsmpurl address
Encoding encode = system. Text. encoding. getencoding ("UTF-8 ");
Byte [] arrb = encode. getbytes (sxmlmessage );
Httpwebrequest myreq = (httpwebrequest) webrequest. Create (dsmpurl );
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 ();

// Receives the HTTP Response
Webresponse myresp = myreq. getresponse ();
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 ();

Public static string postdata (string Str)
{
Try
{
Byte [] DATA = system. Text. encoding. getencoding ("gb2312"). getbytes (STR );
// Prepare the request...
Httpwebrequest Req = (httpwebrequest) webrequest. Create (lmatrix URL );
Req. method = "Post ";
Req. contenttype = "application/X-WWW-form-urlencoded ";
Req. contentlength = data. length;
Stream stream = Req. getrequeststream ();
// Send data
Stream. Write (data, 0, Data. Length );
Stream. Close ();

Httpwebresponse rep = (httpwebresponse) Req. getresponse ();
Stream receivestream = rep. getresponsestream ();
Encoding encode = system. Text. encoding. getencoding ("gb2312 ");
// Pipes the stream to a higher level stream reader with the required encoding format.
Streamreader readstream = new streamreader (receivestream, encode );

Char [] READ = new char [256];
Int COUNT = readstream. Read (read, 0,256 );
Stringbuilder sb = new stringbuilder ("");
While (count> 0)
{
String readstr = new string (read, 0, count );
SB. append (readstr );
Count = readstream. Read (read, 0,256 );
}

Rep. Close ();
Readstream. Close ();

Return sb. tostring ();

}
Catch (exception ex)
{
Return "";
Forumexceptions. Log (Ex );
}
}

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.