Asp. NET sends data to a Web page on a post basis

Source: Internet
Author: User
Tags interface net return string
Project users put forward new requirements, the original project within the platform to send information sent to the mobile phone, fortunately they already have a message sent platform, as long as the call its interface to send it.

The SMS delivery interface is a Web page that is implemented using JSP, which is called to send data to the Web page in a post manner.

Finding data on the web is almost always the same result:

System.Net.WebRequest req = System.Net.WebRequest.Create (URI);

Req. Proxy = new System.Net.WebProxy (proxystring, true);

And then according to user-given interface description and Java example modification, the result is always return the result is garbled, and then to the online search, said is the coding way of the problem, there is no way, only a number of attempts. After nearly a day of continuous testing, finally succeeded. My correct code is as follows:

Protected string sendmsg (String xmlmsg)

{

String urlpage = "http://www.handtimes.com/interface/forSCMIS.jsp";

Stream outstream = null;

Stream instream = null;

StreamReader sr = null;

HttpWebResponse response = null;

HttpWebRequest request = null;

Note that this is the encoding method that I've been trying for a long time and the content of the XML content encoding

Encoding Encoding = encoding.getencoding ("GBK");

byte[] data = encoding. GetBytes (XMLMSG);

Prepare request ...

Setting parameters

Request = WebRequest.Create (urlpage) as HttpWebRequest;

Request. method = "POST";

The content type of this place is required on the interface document and must be the case

Request. ContentType = "Text/plain";

Request. ContentLength = data. Length;

OutStream = Request. GetRequestStream ();

OutStream. Write (data, 0, data. Length);

OutStream. Flush ();

OutStream. Close ();

Send request and get corresponding response data

Response = Request. GetResponse () as HttpWebResponse;

Until request. The GetResponse () program only starts sending post requests to the destination Web page

Instream = Response. GetResponseStream ();

sr = new StreamReader (instream, encoding);

Return results page (HTML) code

String content = Sr. ReadToEnd ();

return content;

}

To illustrate, the encoding of the data to be sent and the encoding of the content (XML) that was sent was successful using the GBK encoding,

Because the user to my account can not be sent to my own mobile phone, so I do not dare to do too many attempts, after the success did not continue to try, do not know the impact of the return of the content is garbled which code, or two are affected.

Req. ContentType = "application/x-www-form-urlencoded";

Req. method = "POST";

byte [] bytes = System.Text.Encoding.ASCII.GetBytes (Parameters);

Req. contentlength = bytes. Length;

System.IO.Stream OS = req. GetRequestStream ();

Os. Write (bytes, 0, bytes. Length);

Os. Close ();

System.Net.WebResponse resp = req. GetResponse ();

if (resp== null) return null;

System.IO.StreamReader sr = new System.IO.StreamReader (resp. GetResponseStream ());

return Sr. ReadToEnd (). Trim ();



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.