手机
当前位置:查字典教程网 >编程开发 >C#教程 >C#发送HttpPost请求来调用WebService的方法
C#发送HttpPost请求来调用WebService的方法
摘要:复制代码代码如下:voidUpdateContactSign(){stringServerPage="http://localhost/We...

复制代码 代码如下:

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;

}

【C#发送HttpPost请求来调用WebService的方法】相关文章:

C# javascript 读写Cookie的方法

C# 向二进制文件进行读写的操作方法

C#对XML文件的各种操作实现方法

C# dynamic关键字的使用方法

C#访问应用程序配置文件的方法

C#中将ListView中数据导出到Excel的实例方法

C#中一些你可能没用过的调试窗口的方法

C#实现窗体淡入淡出效果的方法总结

重写、隐藏基类(new, override)的方法

C# 格式化字符首字母大写的方法

精品推荐
分类导航