. NET through the URL push post data concrete implementation

Source: Internet
Author: User

  This article mainly introduces the implementation of. NET push post data via URL, and a friend in need can refer to

Due to a new company to start to contact with MVC and other things. So I pick up a lot of things again.   A while ago let me write a method of pushing butt with a third party company. Post the data back through the URL provided by the other side.   I put the URL into the Web.config   code as follows: <add key= "urlstrings" value= "urladdress"/>     in the. cs file The   code is as follows : private String poststring = system.configuration.configurationmanager.appsettings["Urlstrings"]. ToString ();     Because I'm sending the data out as XML text, I want to wrap the data and then send the data through the HttpWebRequest request. The 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           &NBsp     {                    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 ();     In this place it is worth my attention that at the beginning my earliest MemoryStream used the stream. But stream data streams can be an inexplicable error. Stream data stream cannot perform length lookup operations     Later I also went through the Internet to find a solution, with MemoryStream to the temporary stream, and finally the stream on some of the search operations on the MemoryStream to carry out, Finally, the data is imported into stream data stream through the MemoryStream CopyTo () method.   The last is the receipt of data, this is simple some code as follows: HttpWebResponse hwresponse = (httpwebresponse) hwr. GetResponse ();                     Stream stream = null;                    stream= hwresponse.getresponsestream ();                     StreamReader reader = new StreamReader (Stream, Syste M.text.encoding.default, True);                     String file = Reader. ReadToEnd ();                     utf8encoding UTF = new UTF8Encoding ();                     byte[] Bytes = UTF. GetBytes (file);                     file = UTF. GetString (Bytes); &nbSp   This place has a conversion to the data encoding, I was converted to UTF-8 encoding.   Finally, I received data processing, because I received is also XML text form of data, so there are some processing operations, but also facilitate their own follow-up operations.   Code as follows: HttpWebResponse hwresponse = (httpwebresponse) hwr. GetResponse ();                     Stream stream = null;                    stream= hwresponse.getresponsestream ();                     StreamReader reader = new StreamReader (Stream, Syste M.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 (strb Ody);                         String userId = Getelementvalue (xbody. Element ("Body"). Element ("Useid"));     This is some of the applications I use this time.   I am a novice, please advise 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.