12,162
社区成员
发帖
与我相关
我的任务
分享
string res = sendMsg("http://localhost:5553/webUI/webservice.asmx/HelloWorld", "");//ok 返回正常
res = sendMsg("http://localhost:5553/webUI/webservice.asmx/HelloWorld", getPostData("aa","bb"));//异常,有时返回500错误,有时返回连接关闭
//方法
private string sendMsg(string url, string postData)
{
//string url = "";
//tel=223344&switch=5623464&province=gdgmcc&city=gdfhda&ptime=20080526013012&cmds=gasjkgja&content=gkasjlgj
//name = System.Web.HttpUtility.UrlEncode(name);
Encoding encoding = Encoding.GetEncoding("utf-8");
byte[] data = encoding.GetBytes(postData);
// 准备请求...
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
// 发送数据
newStream.Write(data, 0, data.Length);
newStream.Close();
//接收服务器返回数据
Stream responseStream;
string stringResponse = string.Empty;
try
{
responseStream = myRequest.GetResponse().GetResponseStream();
//string stringResponse = string.Empty;
using (StreamReader responseReader =
new StreamReader(responseStream, Encoding.GetEncoding("gb2312")))
{
stringResponse = responseReader.ReadToEnd();
}
responseStream.Close();
//if (!stringResponse.Trim().Equals("OK")) // 发送失败,写日志
//{
// TmiLog.write("", "");
//}
}
catch (Exception ee)
{
return "";
}
return stringResponse;
}
private string getPostData(string id, string name)
{
StringBuilder postData =new StringBuilder ();
postData.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
postData.Append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
postData.Append("<soap:Body>");
postData.Append("<getInfo xmlns=\"http://tempuri.org/\">");
postData.Append("<info>");
postData.Append("<ID>" + id + "</ID>");
postData.Append("<Name>" + name + "</Name>");
postData.Append("</info></getInfo></soap:Body>");
postData.Append("</soap:Envelope>");
return postData.ToString();
}
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
/// <summary>
/// WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService
{
public WebService()
{
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public string getInfo(ReturnInfo info)
{
return info.ID + info.Name;
}
[Serializable]
public class ReturnInfo
{
private string _id = "";
private string _name = "";
public string ID
{
set { this._id = value; }
get { return this._id; }
}
public string Name
{
set { this._name = value; }
get { return this._name; }
}
}
}
//call
soapCallWS("http://localhost:5553/webUI/webservice.asmx", getPostData("hi,","welcome to our company"));
//method
private void soapCallWS(string url, string soap)
{
//发起请求
Uri uri = new Uri(url);
WebRequest webRequest = WebRequest.Create(uri);
webRequest.ContentType = "text/xml; charset=utf-8";
webRequest.Method = "POST";
using (Stream requestStream = webRequest.GetRequestStream())
{
byte[] paramBytes = Encoding.UTF8.GetBytes(soap.ToString());
requestStream.Write(paramBytes, 0, paramBytes.Length);
}
string xml = "";
//响应
WebResponse webResponse = webRequest.GetResponse();
using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
{
xml = myStreamReader.ReadToEnd();
}
}