c# webservice的三种请求方式
定义webservice接口
如下,第一个支持所有请求,第二个仅支持http post
[WebMethod]
public string execute(string funcId, string request)
{
JObject json = (JObject)JsonConvert.DeserializeObject(request);
var data = new {
output = json["input"].ToString(),
status=1
};
return JsonConvert.SerializeObject(data);
}
[WebMethod]
[ScriptMethod(UseHttpGet =false)] //只支持http Post 请求方式
public string executePost(string funcId, string request)
{
JObject json = (JObject)JsonConvert.DeserializeObject(request);
var data = new
{
output = json["input"].ToString(),
status = 1
};
return JsonConvert.SerializeObject(data);
}
Soap请求
Uri uri = new Uri("http://localhost:4545/WebService1.asmx");
using (HttpClient client=new HttpClient())
{
var pargrams = JsonConvert.SerializeObject(new
{
input = new { a = 1, b = 2 }
});
client.DefaultRequestHeaders.Add("SOAPAction", "http://tempuri.org/execute"); // 访问的的接口
StringBuilder str = new StringBuilder();
str.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
str.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/\">");
str.Append("<soap:Body>");
str.Append("<execute xmlns=\"http://tempuri.org/\">");
str.Append("<funcId>\"cs123456\"</funcId>");
str.Append("<request>" + pargrams + "</request>");
str.Append("</execute>");
str.Append("</soap:Body>");
str.Append("</soap:Envelope>");// 拼接请求参数
StringContent content = new StringContent(str.ToString(), Encoding.UTF8, "text/xml");//指定字符编码和媒体格式
HttpResponseMessage response = await client.PostAsync(uri, content);// 发送请求
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(response.Content.ReadAsStringAsync().Result);//解析xml数据,获取返回内容
LogWrite("【Soap】: " + xmlDocument.InnerText);
this.textBox1.AppendText(DateTime.Now+"【Soap】: "+xmlDocument.InnerText+"\r\n");
}
Http Post请求
Uri uri = new Uri("http://localhost:4545/WebService1.asmx/executePost");
using (HttpClient client = new HttpClient())
{
var pargrams = JsonConvert.SerializeObject(new
{
input = new { a = 1, b = 2 }
});
Dictionary<string, string> dic = new Dictionary<string, string>
{
{ "funcId", "cs123456" },
{ "request", pargrams },
};
HttpContent content = new FormUrlEncodedContent(dic);// 拼接参数 形式为 funcid=string&requst=string
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded");//媒体格式
content.Headers.ContentLength = Encoding.UTF8.GetByteCount(await content.ReadAsStringAsync());// 内容长度赋值
HttpResponseMessage response = await client.PostAsync(uri, content);
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(response.Content.ReadAsStringAsync().Result);
LogWrite("【Post】: " + xmlDocument.InnerText);
this.textBox1.AppendText(DateTime.Now + "【Post】: " + xmlDocument.InnerText+"\r\n");
};
直接服务引用
2.
-
var pargrams = JsonConvert.SerializeObject(new { input = new { a = 1, b = 2 } }); WebService1SoapClient client = new WebService1SoapClient();//需要引入 默认是 using RequestWebService.ServiceReference1; LogWrite("【服务】: " + client.execute("cs123456", pargrams)); this.textBox1.AppendText(DateTime.Now + "【服务】: " + client.execute("cs123456", pargrams) + "\r\n");