代码之家  ›  专栏  ›  技术社区  ›  EEE

如何在服务器端从HTTPPOST请求加载数据?

  •  1
  • EEE  · 技术社区  · 16 年前

    [OperationContract]
    
        [WebInvoke(
    
            Method = "POST",
            UriTemplate = "/go",
            BodyStyle = WebMessageBodyStyle.Bare,
            RequestFormat = WebMessageFormat.Xml,
            ResponseFormat = WebMessageFormat.Xml
    
        )]       
        string go(string name);
    

    我发布的数据是这样的;

    System.Net.WebClient client = new System.Net.WebClient();
    
            string reply = client.UploadString(
                "http://localhost/HelloService/Service.svc/go",
                "POST",
                aString);
    

    问题是,我如何从发布的消息中获取数据 go() 方法,而不使用这样的uri模板;

    UriTemplate = "/go({name})"
    

    2 回复  |  直到 16 年前
        1
  •  1
  •   EEE    16 年前

    这就是解决办法;

    [OperationContract]
    [WebInvoke(
    BodyStyle = WebMessageBodyStyle.Wrapped,
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json,
    Method = "POST",
    UriTemplate="/go"
    )]  
    string MyWebServiceMethod(string name);
    

    HTTP POST请求是;

    POST /go Modify HTTPS/1.1
    Content-Type: application/json; charset=utf-8
    Host: WebserviceURL
    Content-Length: length
    
    {"name":"someName"}
    
        2
  •  0
  •   Ria    13 年前

    你有权访问吗 System.Web.HttpContext.Current ?

    您可以尝试System.Web.HttpContext.Current.Request.Form,或者,看起来您正在使用XML作为请求格式,因此您可能希望尝试从请求流加载XElement或XmlDocument。

    (此处可能有错误的方法,但它存在于请求对象中的某个位置) //仅限.NET 4.0

    XElement el = XElement.Load(HttpContext.Current.Request.GetRequestStream());
    
    推荐文章