Push 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.

<Add key = value =/>View Code

In the. CS File

PostString = System. Configuration. ConfigurationManager. receivettings []. ToString ();View Code

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.

Body =. format (, consumption. id, consumption. order. agentOrderId, consumption. count, consumption. createTime. dateTimeToDateString (request = HttpWebRequest hwr = hwr. method = hwr. headers. add (, HttpUtility. urlEncode (hwr. contentType = hwr. allowAutoRedirect = [] dates = count = MemoryStream newStream = log. add (newStream. write (dates, hwr. contentLength = Stream requestStream = newStream. position =View Code

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.

HttpWebResponse hwResponse = Stream stream = stream reader = StreamReader (stream, System. text. encoding. default, file = UTF8Encoding UTF = Byte [] Bytes = file = UTF. getString (Bytes );View Code

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.

HttpWebResponse hwResponse = Stream stream = stream reader = StreamReader (stream, System. text. encoding. default, file = UTF8Encoding UTF = Byte [] Bytes = file = strBody = TCodeServiceCrypt. decrypt3DESFromBase64 (GetElementValue (doc. element (). element (XDocument xBody = userId = GetElementValue (xBody. element (). element ());View Code

This is some of the applications I used this time.

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.