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.