您的位置:首页 > 理论基础 > 计算机网络

Use Http Post to call Web Service

2016-07-30 13:47 393 查看

[WebMethod]

public bool IsUserExist(string userName) {

if (userName == "raymond")

return true;

else

return false;

}


protected void Button2_Click(object sender, EventArgs e)

{

byte[] data = System.Text.Encoding.ASCII.GetBytes("userName=" + TextBox1.Text);



System.Net.WebRequest request = System.Net.HttpWebRequest.Create(

"http://localhost/samples/MyService.asmx/IsUserExist");

request.Method = "POST";

request.ContentLength = data.Length;

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



System.IO.Stream str = request.GetRequestStream();

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

str.Flush();

System.Net.WebResponse response = request.GetResponse();

System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream());

string result = reader.ReadToEnd();



TextBox2.Text = result; // the result here is xml format, you need to handle this

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: