一、Webservice调用的三种方式:Webservice调用1:浏览器中输入以下URL,然后再返回的页面输入参数,提交实现调用http://localhost/WebService/MyService.asmx?op=MyActionWebservice调用2:html中通过 ...
一、Webservice调用的三种方式:
Webservice调用1:浏览器中输入以下URL,然后再返回的页面输入参数,提交实现调用
void UpdateContactSign() { get='_blank'>string ServerPage ="http://localhost/WebService/MyService.asmx"; try { //ServerPage += "?op=TangramAction"; ServerPage += "/MyAction";//MyAction是WebService中的方法 string str"<a ObjID=\"9\"></a>",;//第一个参数 string strData="ContactSign|990011|我的数据";//第二个参数 string res = HttpConnectToServer(ServerPage, str//MessageBox.Show(res); } catch (Exception ex) { } } //发送消息到服务器 public string HttpConnectToServer(string ServerPage,string strstring strData) { string postData = "str" + str"&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请求参数传值的中文乱码处理:
1.客户端参数Base64编码处理 在给postData赋值之前,先对 strData进行Base64编码处理,注意加号(’+‘)的替换处理,否则由于加号经过Url传递后变成空格而得不到合法的Base64字符串,修改代码如下:
public string MyAction(string strstring strData) { //Base64解码 byte[] buffer = Convert.FromBase64String(strData); string Data = Encoding.Default.GetString(buffer); //方法实现部分代码中凡用到strData的地方 改为使用解码后的Data ........ ........ }
原标题:C#发送HttpPost请求调用WebService
关键词:C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。