C # Send a HttpPost request to invoke the WebService method _c# tutorial

Source: Internet
Author: User

Copy Code code as follows:

void Updatecontactsign ()
{
String serverpage = "Http://localhost/WebService/MyService.asmx";
Try
{
Serverpage = "Op=tangramaction";
Serverpage + = "/myaction";//myaction is the method in WebService
String strxml= "<a objid=\" 9\ "></a>",;//First parameter
String Strdata= "contactsign|990011| my Data";//second parameter
string res = Httpconnecttoserver (serverpage, Strxml, strdata);
MessageBox.Show (RES);
}
catch (Exception ex)
{

}
}

Send Message to 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 an input stream
Stream dataStream = null;
Try
{
DataStream = Request. GetRequestStream ();
}
catch (Exception)
{
Return null;//Connection Server failed
}

Send Request
Datastream.write (DataArray, 0, dataarray.length);
Datastream.close ();
Read return 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;//Connection Server failed
}
return res;
}

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.