C # Send an HttpPost request to call WebService,

Source: Internet
Author: User

C # Send an HttpPost request to call WebService,

I. Webservice call methods:

Webservice call 1: Enter the following URL in the browser, then enter parameters on the returned page, and submit the request for Implementation call.
Http: // localhost/WebService/MyService. asmx? Op = MyAction

Webservice call 2: html is called through form submission

<Form id = "form1" runat = "server" action = "http: // 10.15.223.56/WebService/MyService. asmx/MyAction "> <input type =" text "name =" strXml "value =" <a ObjID = \ "9 \"> </a> "/>
<Input type = "text" name = "strData" value = "ContactSign | 990011 | my data"/>
<Input type = "submit" value = "Send"/>
</Form>

Webservice call 3: Send an HttpPost request in C # To Call The MyAction method in WebService. The Code is as follows:

Void UpdateContactSign () {string ServerPage =" http://localhost/WebService/MyService.asmx "; Try {// ServerPage + = "? Op = TangramAction "; ServerPage + ="/MyAction "; // MyAction is the WebService method string strXml = "<a ObjID = \" 9 \ "> </a> ",; // The first parameter string strData = "ContactSign | 990011 | my data"; // The second parameter string res = HttpConnectToServer (ServerPage, strXml, strData); // MessageBox. show (res);} catch (Exception ex) {}} // send the message to the server public string HttpConnectToServer (string ServerPage, string strXml, string strData) {string postData = "strXml =" + strXml + "& strData =" + strData; byte [] dataArray = Encoding. default. getBytes (postData); // create a request HttpWebRequest request = (HttpWebRequest) HttpWebRequest. create (ServerPage); request. method = "POST"; request. contentLength = dataArray. length; request. contentType = "application/x-www-form-urlencoded"; // create input Stream dataStream = null; try {dataStream = request. getRequestStream ();} catch (Exception) {return null; // failed to connect to the server} // send the request dataStream. write (dataArray, 0, dataArray. length); dataStream. close (); // read the returned message string res = string. empty; try {HttpWebResponse response = (HttpWebResponse) request. getResponse (); StreamReader reader = new StreamReader (response. getResponseStream (), Encoding. UTF8); res = reader. readToEnd (); reader. close () ;}catch (Exception ex) {return null; // failed to connect to the server} return res ;}

 

Ii. garbled Chinese characters in the parameters of the HttpPost request sent in C:

1. client parameter Base64 encoding before assigning a value to postData, perform Base64 encoding on strData. Pay attention to replacing the plus sign ('+, otherwise, the Base64 string cannot be obtained because the plus sign is passed through the Url and becomes a space. The modification code is as follows:

// Send the message to the server public string HttpConnectToServer (string ServerPage, string strXml, string strData) {// Base64 Encoding processes Chinese garbled byte [] buffer = Encoding. default. getBytes (strData); string Base64StrData = Convert. toBase64String (buffer ). replace ("+", "% 2B"); // process the plus string postData = "strXml =" + strXml + "& strData =" + Base64StrData; byte [] dataArray = Encoding. default. getBytes (postData); // create a request
.......
...... Return res;
}

2. After the server obtains the data, it decodes the data before using it. The decoded code is as follows:

Public string MyAction (string strXml, string strData) {// Base64 decoded byte [] buffer = Convert. fromBase64String (strData); string Data = Encoding. default. getString (buffer); // The method is used in some codes. If strData is used, the decoded Data ......} is used ................}

 

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.