HttpWebRequest post Application Instances

Source: Internet
Author: User

This article uses three examples to introduce the usage of HttpWebRequest post in asp.net and the convenience methods in various places. If you need it, you can refer to this article.


First look at a simple measure

The Code is as follows: Copy code

Using System;
Using System. IO;
Using System. Collections;
Using System. Data;
Using System. Linq;
Using System. Web;
Using System. Web. Services;
Using System. Web. Services. Protocols;
Using System. Xml. Linq;
Using System. Net;
Using System. Text;

Namespace WebApplication1
{

[WebService (Namespace = "http://www.hzhuti.com/")]
[WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)]
Public class Handler1: IHttpHandler
{

Public void ProcessRequest (HttpContext context)
{
HttpWebRequest req = (HttpWebRequest) HttpWebRequest. Create (http://www.bKjia. c0m );
Encoding encoding = Encoding. UTF8;
String param = "ie = UTF-8 & source = txt & query = hello & t = 1327829764203 & token = 8a7dcbacb3ed72cad9f3fb079809a127 & from = auto & to = auto ";
// Encoding. GetBytes (postData );
Byte [] bs = Encoding. ASCII. GetBytes (param );
String responseData = String. Empty;
Req. Method = "POST ";
Req. ContentType = "application/x-www-form-urlencoded ";
Req. ContentLength = bs. Length;
Using (Stream reqStream = req. GetRequestStream ())
{
ReqStream. Write (bs, 0, bs. Length );
ReqStream. Close ();
}
Using (HttpWebResponse response = (HttpWebResponse) req. GetResponse ())
{
Using (StreamReader reader = new StreamReader (response. GetResponseStream (), encoding ))
{
ResponseData = reader. ReadToEnd (). ToString ();
}
Context. Response. Write (responseData );
}
}
Public bool IsReusable
{
Get
{
Return false;
}
}
}
}


HttpWebRequest sends a POST request for Automatic Login

The Code is as follows: Copy code

<Form name = "form1" action = "http: www.breakn.com/login.asp" method = "post">
<Input type = "text" name = "userid" value = ">
<Input type = "password" name = "password" value = "">
</Form>

From the form, we can see that the form has two form fields, one is userid and the other is password. Therefore, data submitted in the form of POST should contain these two fields.
The data format of POST is as follows:
Form domain name 1 = value 1 & form domain name 2 = value 2 & form Domain Name 3 = value 3 ......
Note that the "value" must pass through HTMLEncode, that is, it cannot contain the "<>=&" symbols.

The data to be submitted in this example should be:

The Code is as follows: Copy code

Userid = value1 & password = value2
String strId = "guest ";
String strPassword = "123456 ";

ASCIIEncoding encoding = new ASCIIEncoding ();
String postData = "userid =" + strId;
PostData + = ("& amp; password =" + strPassword );

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

// Prepare web request...
HttpWebRequest myRequest =
(HttpWebRequest) WebRequest. Create ("http: www.here.com/login.asp ");

MyRequest. Method = "POST ";
MyRequest. ContentType = "application/x-www-form-urlencoded ";
MyRequest. ContentLength = data. Length;
Stream newStream = myRequest. GetRequestStream ();

// Send the data.
NewStream. Write (data, 0, data. Length );
NewStream. Close ();

// Get response
HttpWebResponse myResponse = (HttpWebResponse) myRequest. GetResponse ();
StreamReader reader = new StreamReader (response. GetResponseStream (), Encoding. Default );
String content = reader. ReadToEnd ();
Console. WriteLine (content );


When using HttpWebRequest POST data to the HTTP protocol on the server, the following parameters are transmitted to the server: HTTP Header/GET String/Body in the middle of the POST
The following sample code demonstrates how to simply POST data to the server.

The Code is as follows: Copy code

// Send the request
String requestBody = string. format ("{0 }={ 1} & {2 }={ 3} & {4 }={ 5} & {6 }={ 7} & {8 }= {9 }"
, HttpUtility. UrlEncode ("version_id", Encoding. GetEncoding ("GB2312 "))
, HttpUtility. UrlEncode (m_VersionNo, Encoding. GetEncoding ("GB2312 "))
, HttpUtility. UrlEncode ("merchant_id", Encoding. GetEncoding ("GB2312 "))
, HttpUtility. UrlEncode (m_MerchantID, Encoding. GetEncoding ("GB2312 "))
, HttpUtility. UrlEncode ("verifystring", Encoding. GetEncoding ("GB2312 "))
, HttpUtility. UrlEncode (hashStr. ToString (). ToLower (), Encoding. GetEncoding ("GB2312 "))
, HttpUtility. UrlEncode ("order_date", Encoding. GetEncoding ("GB2312 "))
, HttpUtility. UrlEncode (m_OrderDate, Encoding. GetEncoding ("GB2312 "))
, HttpUtility. UrlEncode ("order_id", Encoding. GetEncoding ("GB2312 "))
, HttpUtility. UrlEncode (m_OrderID, Encoding. GetEncoding ("GB2312 "))
, HttpUtility. UrlEncode ("retmode", Encoding. GetEncoding ("GB2312 "))
, String. Empty
);

HttpWebRequest request = (HttpWebRequest) HttpWebRequest. Create (m_GatewayURL );
Request. Method = "POST ";
Request. KeepAlive = false;
Request. ContentType = "application/x-www-form-urlencoded ";
Byte [] aryBuf = Encoding. GetEncoding ("GB2312"). GetBytes (requestBody );
Request. ContentLength = aryBuf. Length;
Using (Stream writer = request. GetRequestStream ())
{
Writer. Write (aryBuf, 0, aryBuf. Length );
Writer. Close ();
Writer. Dispose ();
}
String ret = string. Empty;
Using (WebResponse response = request. GetResponse ())
{
StreamReader reader = new StreamReader (response. GetResponseStream ()
, Encoding. GetEncoding ("GB2312 ")
);
Ret = reader. ReadToEnd ();
Reader. Close ();
Reader. Dispose ();
}

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.