【问题标题】:How to load data from HTTPPOST request in server side?如何在服务器端从 HTTPPOST 请求加载数据?
【发布时间】:2009-11-25 12:36:04
【问题描述】:

我有一个这样的 WebInvoke 方法;

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

因为我要发送大量数据,无法在uri模板中发送

【问题讨论】:

    标签: c# wcf post uri webinvoke


    【解决方案1】:

    这是解决方案;

    [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】:

    你可以访问System.Web.HttpContext.Current吗?

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

    (这里可能方法不对,但存在于请求对象的某处) // 仅限 .NET 4.0

    XElement el = XElement.Load(HttpContext.Current.Request.GetRequestStream());
    

    【讨论】:

    • 我无法访问 System.Web.HttpContext.Current。需要.Net4.0吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-03
    • 1970-01-01
    • 1970-01-01
    • 2014-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多