. Net pushes POST data through URL

Source: Internet
Author: User

As a new company is getting started with MVC and other things. So I picked up a lot of things again.

Some time ago, I was asked to write a method for connecting to a third-party company push. Push the data post through the URL provided by the other party.

I moved the url to web. config.

Copy codeThe Code is as follows:
<Add key = "urlStrings" value = "urladdress"/>

In the. CS File

Copy codeThe Code is as follows:
Private string postString = System. Configuration. ConfigurationManager. receivettings ["urlStrings"]. ToString ();

Because I want to transfer the data in xml format, we need to package the data and then send the data through the HttpWebRequest request.

Copy codeThe Code is as follows:
String body = string. Format (@ "<? Xml version = "" 1.0 "" encoding = "" UTF-8 ""?>
<Body>
<ValidId> {0} </ValidId>
<OrderId> {1} </OrderId>
<Count> {2} </Count>
<ValidTime >{3 }</ValidTime>
<Remark/>
</Body> ", consumption. id, consumption. order. agentOrderId, consumption. count, consumption. createTime. dateTimeToDateString ("yyyy-MM-dd HH: mm: ss "));

String request = BuildRequest (body );

HttpWebRequest hwr = (HttpWebRequest) WebRequest. Create (postString );
Hwr. Method = "POST ";
Hwr. Headers. Add ("X-Auth-Token", HttpUtility. UrlEncode ("openstack "));
Hwr. ContentType = "application/json ";
// Hwr. Accept = "application/xml ";
Hwr. AllowAutoRedirect = true;

Byte [] dates = Encoding. UTF8.GetBytes (request );
Int count = dates. Length;
// Stream newStream = hwr. GetRequestStream ();
MemoryStream newStream = new MemoryStream ();
Try
{
Log. Add ("START request ");
NewStream. Write (dates, 0, dates. Length );
Hwr. ContentLength = newStream. Length;
Stream requestStream = hwr. GetRequestStream ();
NewStream. Position = 0L;
NewStream. CopyTo (requestStream );
NewStream. Close ();
RequestStream. Close ();

It is worth noting that at the beginning, the earliest MemoryStream used Stream. However, Stream data streams may report errors inexplicably. Stream Data Streams cannot perform length search.

Later, I found a solution through online search. I used MemoryStream to temporarily represent Stream. Finally, I put some search operations on Stream on MemoryStream. Finally, I used CopyTo () of MemoryStream () method To import data into Stream data streams.

The last step is to receive data, which is simpler.

Copy codeThe Code is as follows:
HttpWebResponse hwResponse = (HttpWebResponse) hwr. GetResponse ();
Stream stream = null;
Stream = hwResponse. GetResponseStream ();
StreamReader reader = new StreamReader (stream, System. Text. Encoding. Default, true );
String file = reader. ReadToEnd ();
UTF8Encoding UTF = new UTF8Encoding ();
Byte [] Bytes = UTF. GetBytes (file );
File = UTF. GetString (Bytes );

This place has a conversion to data encoding, I am converting to UTF-8 encoding.

Finally, I am processing the received data. Because I am receiving xml text data, I still have some processing operations to facilitate subsequent operations.

Copy codeThe Code is as follows:
HttpWebResponse hwResponse = (HttpWebResponse) hwr. GetResponse ();
Stream stream = null;
Stream = hwResponse. GetResponseStream ();
StreamReader reader = new StreamReader (stream, System. Text. Encoding. Default, true );
String file = reader. ReadToEnd ();
UTF8Encoding UTF = new UTF8Encoding ();
Byte [] Bytes = UTF. GetBytes (file );
File = UTF. GetString (Bytes );
String strBody = TCodeServiceCrypt. Decrypt3DESFromBase64 (GetElementValue (doc. Element ("Response"). Element ("Body"), UserFunc. SecretKey );
XDocument xBody = XDocument. Parse (strBody );
String userId = GetElementValue (xBody. Element ("Body"). Element ("UseId "));

This is some of the applications I used this time.

I am a newbie. Please advise me more.

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.