12,162
社区成员
发帖
与我相关
我的任务
分享
httpWebRequest = (HttpWebRequest)WebRequest.Create(this.ServiceUrl);
httpWebRequest.Headers["Accept-Encoding"] = "deflate";
httpWebRequest.Headers["SOAPAction"] = string.Empty;
httpWebRequest.Accept = "text/xml";
httpWebRequest.ContentType = "text/xml";
httpWebRequest.Method = "POST";
var rb = Encoding.UTF8.GetBytes(this.Message);
httpWebRequest.ContentLength = rb.Length;
using (var requestStream = httpWebRequest.GetRequestStream())
{
requestStream.Write(rb, 0, rb.Length);
}
using (var response = httpWebRequest.GetResponse())
using (var responseStream = response.GetResponseStream())
{
if (responseStream == null)
{
return new SOAPResponse
{
Status = ResponseStatus.Success,
};
}
using (var reader = new StreamReader(responseStream))
{
return new SOAPResponse
{
Status = ResponseStatus.Success,
Message = reader.ReadToEnd()
};
}
}
MyParam.AppendLine("<soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope' xmlns:bai='http://www.baidu.com/'>");
[WebService(Namespace = "http://www.baidu.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[SoapDocumentService(RoutingStyle=SoapServiceRoutingStyle.RequestElement)]
[WebMethod(Description = "测试用,不做处理直接返回输入的内容")]
public String ReturnTest(String usr, String pwd)
{
return "您输入了:‘"+usr + "’ 和 ‘" + pwd +"’";
}
2、定义接口和接口参数(如果这里的接口和参数不了解,可以通过soapui工具自动生成)
StringBuilder MyParam
MyParam.AppendLine("<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:bai="http://www.baidu.com/">");
MyParam.AppendLine("<soap:Header/>");
MyParam.AppendLine("<soap:Body>");
MyParam.AppendLine("<bai:ReturnTest>");
MyParam.AppendLine("<bai:usr>A008</bai:usr>");
MyParam.AppendLine("<bai:pwd>123456</bai:pwd>");
MyParam.AppendLine("</bai:ReturnTest>");
MyParam.AppendLine("</soap:Body>");
MyParam.AppendLine("</soap:Envelope>");
3、HttpWebRequest 调用方法
public string GetGsReturn(string url, StringBuilder param)//提交到接口并返回参数
{
string responseString = string.Empty;
try
{
byte[] bs = Encoding.UTF8.GetBytes(param.ToString()); //具体根据实际的编码来修改
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = "post";
//---------------这个根据实际情况来填写-----------------------
myRequest.Headers.Add("SOAPAction", "");
myRequest.Headers.Add("Accept-Encoding", "gzip,deflate");
myRequest.ContentType = "text/xml; charset=utf-8";
myRequest.KeepAlive = true;
myRequest.ContentLength = bs.Length;
//-------------------------------------------------------------
Stream reqStream = myRequest.GetRequestStream();
reqStream.Write(bs, 0, bs.Length);
HttpWebResponse myResponse;
try
{ myResponse = (HttpWebResponse)myRequest.GetResponse(); }
catch (WebException ex)
{ myResponse = (HttpWebResponse)ex.Response; }
if (myRequest != null)
{
StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
responseString = sr.ReadToEnd();
}
}
catch (Exception ex)
{
responseString = ex.Message;
}
return responseString;
}
4、调用
GetGsReturn("http://192.168.1.108/Webservice.asmx",MyParam)