Manually send HTTP request to call Web Service

Source: Internet
Author: User
Tags urlencode

What if I cannot directly add a reference to a web service that is not called in. Net? The following does not directly implement
Service.

Method 1: Get call

 
 
    1. Private void button#click (Object sender, eventargs E) 2. {3. String strurl = "http: // localhost: 12074/service1.asmx/4. getproductprice? Productid = "; 5. strurl + = This. textbox1.text; 6. // create an HTTP request. httpwebrequest request = (httpwebrequest) webrequest. create (strurl); 8. // request. method = "get"; 9. httpwebresponse response = (system. net. httpwebresponse) request. getresponse (); 10. stream S = response. getresponsestream (); 11. // convert to XML and process it by yourself. 12. xmltextreader reader = new xmltextreader (s); 13. reader. movetocontent (); 14. string strvalue = reader. readinnerxml (); 15. strvalue = strvalue. replace ("<", "<"); 16. strvalue = strvalue. replace (">", ">"); 17. messageBox. show (strvalue); 18. reader. close (); 19 .}

Method 2: Post call

 
 
    1. private void button2_click (Object sender, eventargs E) 2. {3. string strurl = "http: // localhost: 12074/service1.asmx/getproductprice"; 4. // create an HTTP request. httpwebrequest request = (httpwebrequest) webrequest. create (strurl); 6. // POST Request Method 7. request. method = "Post"; 8. // content type 9. request. contenttype = "application/X-WWW-form-urlencoded"; 10. // set parameters and perform URL encoding 11. string paraurlcoded = httputility. urlencode ("productid"); 12. paraurlcoded + = "=" + httputility. urlencode (this. textbox1.text); 13. byte [] payload; 14. // convert the URL-encoded string to byte 15. payload = system. text. encoding. utf8.getbytes (paraurlcoded); 16. // set the request contentlength 17. request. contentlength = payload. length; 18. // send the request to get the request stream 19. stream writer = request. getrequeststream (); 20. // Write Request Parameters to stream 21. writer. write (payload, 0, payload. length); 22. // closes the request stream 23. writer. close (); 24. // obtain the response stream 25. httpwebresponse response = (httpwebresponse) request. getresponse (); 26. stream S = response. getresponsestream (); 27. // convert to XML and process it by yourself. 28. xmltextreader reader = new xmltextreader (s); 29. reader. movetocontent (); 30. string strvalue = reader. readinnerxml (); 31. strvalue = strvalue. replace ("<", "<"); 32. strvalue = strvalue. replace (">", ">"); 33. messageBox. show (strvalue); 34. reader. close (); 35 .}

The main difference between a GET request and a POST request is that the post parameter must be URL encoded and transmitted before the request is obtained.
And get uses URL encoding to directly append the parameter to the request URL.

URL encoding is a character encoding format that ensures that the passed parameters are composed of consistent texts (for example, encoding by spaces ).
Is "% 20 ").

Because the Web
The service returns a standard XML document. Therefore, the basic principle is to process the returned XML data through a webrequest request to obtain the desired information.
At the same time, this method also has the advantage of dynamic calling of different WebService interfaces, which is more flexible.

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.