Use Http Post to call Web Service
鲁烨熠
2023-12-01
[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
}