zoukankan      html  css  js  c++  java
  • WebService 中操作 HttpRequest / HttpResponse (二)[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]

    我们可以通过 HttpContext.Current.Request/Response 将WebService 改造为更佳适合被HttpWebRequest调用的方法。
    先来看看下面简单的示例:

    [WebService(Namespace = "http://tempuri.org/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. [System.Web.Script.Services.ScriptService] public class Service1 : System.Web.Services.WebService {
      [WebMethod]
    public string GetData(string data) {   return "You enter:" + data; } }

    F5运行一下,你会看到一个自动生成的帮助页面,这个帮助页面只有在本地调试时能看到,发布后远程是无法看到这个帮助页面,但可以在

    Web.config 里加上配置再发布即可见。

    <webServices> 
        <protocols> 
            <add name="HttpGet"/>
            <add name="HttpPost"/> 
        </protocols> 
    </webServices>        


    在帮助页面里,你可以看到Http的调用方式:

    你可以按照这个方式用 HTTP POST 调用这个WebMethod:
    【客户端】

    var url = "http://localhost:51013/Service1.asmx/GetData"; 
    var values = new System.Collections.Specialized.NameValueCollection(); values.Add("data", "hi server"); 
    var client = new WebClient(); 
    var data = client.UploadValues(url, "POST", values); 
    var result = System.Text.Encoding.UTF8.GetString(data); 
    Console.WriteLine(result);
    // 输出: // <?xml version="1.0" encoding="utf-8"?> // <string xmlns="http://tempuri.org/">You enter:hi server</string>

    这时 HttpResponse 返回的是一个Xml格式的字符串。


    当然可以控制 Response.ContentType 来返回Xml以外的格式,比如:json, image等
    【服务端】

    [WebMethod] 
    public void GetData2(string title) 
    { 
        var response = HttpContext.Current.Response;         
        response.ContentType = "application/json"; 
        var task = new Task { ID = Guid.NewGuid().ToString("N"), Title = title }; 
        var json = JsonConvert.SerializeObject(task);     
    response.BinaryWrite(System.Text.Encoding.UTF8.GetBytes(json)); }

    【客户端】

    var url2 = "http://localhost:51013/Service1.asmx/GetData2"; 
    var values2 = new System.Collections.Specialized.NameValueCollection(); 
    values2.Add("title", "test title"); 
    var client2 = new WebClient(); 
    var data2 = client.UploadValues(url2, "POST", values2); 
    var result2 = System.Text.Encoding.UTF8.GetString(data2); 
    Console.WriteLine(result2); 
    //输出: //{"ID":"5f836a44f2ee41cb87861843e6aebe37","Title":"test title"}

    如果希望用HTTP GET方式调用WebMethod,则需要使用 ScriptMethod 特性:[ScriptMethod(UseHttpGet=true)],此特性用于指定可从客户端脚本调用的方法的信息。使用此特性可以指定可用于调用方法的 HTTP 谓词(GET 或 POST)。它还使您可以指定是要使用 JSON 还是 XML 对响应进行格式设置。 使用 ScriptMethod 特性时,同时要使用:[System.Web.Script.Services.ScriptService] (Service1类上的特性)

    【服务端】

    [WebMethod] 
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Xml)] 
    public string GetData3(string data) 
    { 
        return "You enter:" + data; 
    }

    【客户端】

    var url3 = "http://localhost:51013/Service1.asmx/GetData3?data='test'"; 
    var client3 = new WebClient(); 
    client3.Headers.Add("Content-Type", "application/json"); 
    var result3 = client3.DownloadString(url3); 
    Console.WriteLine(result3);
    //输出: //You enter:test

    注意:GET调用时是通过Url传值,如果是string需要使用 data='test' 两边加上单引号,因为使用json反序列化。没有引号直接当成是数值转换会出错。(将抛出 500 的服务端错误)


    ScriptMethod 还有一个参数可以控制输出 ResponseFormat,所以没有必要直接控制 Response.ContentType,直接返回即可。
    【服务端】

    [WebMethod] 
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] 
    public Task GetData4(string title) 
    { 
        return new Task { ID = Guid.NewGuid().ToString("N"), Title = title     }; 
    }

    但这样返回的Json并不单纯:)

    {"d":{"__type":"WebSvcHttp.Task","ID":"1b6d28b961324c69a690204bc07922ea","Title" :"test"}}


    一番改造,WebMethod 也能成为 REST 架构风格的 Web API,但是它还无法实现 UrlRewrite,返回的Json也不够单纯,控制也还达不到WCF那么强大。不过简单应用也足够了。

  • 相关阅读:
    494. Target Sum 添加标点符号求和
    636. Exclusive Time of Functions 进程的执行时间
    714. Best Time to Buy and Sell Stock with Transaction Fee有交易费的买卖股票
    377. Combination Sum IV 返回符合目标和的组数
    325. Maximum Size Subarray Sum Equals k 和等于k的最长子数组
    275. H-Index II 递增排序后的论文引用量
    274. H-Index论文引用量
    RabbitMQ学习之HelloWorld(1)
    java之struts2的数据处理
    java之struts2的action的创建方式
  • 原文地址:https://www.cnblogs.com/turnip/p/11764552.html
Copyright © 2011-2022 走看看