Use Http Post to call Web Service

来源:互联网 发布:虚拟机 网络设置 编辑:程序博客网 时间:2024/06/05 05:15

[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

}

0 0
原创粉丝点击