C#发送HttpPost请求来调用WebService的方法
时间:2021-06-11 03:36:30
void UpdateContactSign()
{
string ServerPage ="http://localhost/WebService/MyService.asmx";
try
{
//ServerPage += "?op=TangramAction";
ServerPage += "/MyAction";//MyAction是WebService中的方法
string strXml="<a ObjID=\"9\"></a>",;//第一个参数
string strData="ContactSign|990011|我的数据";//第二个参数
string res = HttpConnectToServer(ServerPage, strXml, strData);
//MessageBox.Show(res);
}
catch (Exception ex)
{
}
}
//发送消息到服务器
public string HttpConnectToServer(string ServerPage,string strXml,string strData)
{
string postData = "strXml=" + strXml+"&strData="+strData;
byte[] dataArray = Encoding.Default.GetBytes(postData);
//创建请求
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(ServerPage);
request.Method = "POST";
request.ContentLength = dataArray.Length;
request.ContentType = "application/x-www-form-urlencoded";
//创建输入流
Stream dataStream = null;
try
{
dataStream = request.GetRequestStream();
}
catch (Exception)
{
return null;//连接服务器失败
}
//发送请求
dataStream.Write(dataArray, 0, dataArray.Length);
dataStream.Close();
//读取返回消息
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;//连接服务器失败
}
return res;
}
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
解决Android软键盘弹出覆盖h5页面输入框问题
![](https://img.aspxhome.com/file/2023/7/83547_0s.jpg)
Spring Boot集成MyBatis实现通用Mapper的配置及使用
SpringBoot 返回Json实体类属性大小写的解决
详解@ConfigurationProperties实现原理与实战
![](https://img.aspxhome.com/file/2023/7/58997_0s.png)
如何利用java控制鼠标操作一些重复的事情
![](https://img.aspxhome.com/file/2023/6/64016_0s.jpg)
Java由浅入深带你精通继承super
![](https://img.aspxhome.com/file/2023/7/59737_0s.png)
java poi解析word的方法
C# WPF如何反射加载Geometry几何图形数据图标
![](https://img.aspxhome.com/file/2023/9/86969_0s.png)
Mybatis延迟加载的实现方式
![](https://img.aspxhome.com/file/2023/8/67018_0s.png)
Java实战之基于swing的QQ邮件收发功能实现
c#消息提示框messagebox的详解及使用
Java实时获取基金收益项目源码分享
![](https://img.aspxhome.com/file/2023/4/85894_0s.png)
SpringBoot整合Kafka工具类的详细代码
java通过ip获取客户端Mac地址的小例子
Eclipse中安装反编译工具Fernflower的方法(Enhanced Class Decompiler)
![](https://img.aspxhome.com/file/2023/1/77361_0s.png)
C#实现简单的计算器小功能
![](https://img.aspxhome.com/file/2023/1/77301_0s.jpg)
Opencv EigenFace人脸识别算法详解
![](https://img.aspxhome.com/file/2023/5/101075_0s.png)
Java日常练习题,每天进步一点点(8)
![](https://img.aspxhome.com/file/2023/9/61879_0s.jpg)