c# webservice的三种请求方式

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"); 
};

直接服务引用

  1. 在这里插入图片描述

2.在这里插入图片描述

  1. 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");
    

案例代码

  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值