当前位置: 首页 > 工具软件 > use-http > 使用案例 >

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

}

 类似资料:

相关阅读

相关文章

相关问答