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

发布于 2024-08-13 06:07:40 字数 733 浏览 8 评论 0原文

我有一个像这样的 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);

问题是如何在不使用这样的 uri 模板的情况下从 go() 方法中获取已发布消息中的数据;

UriTemplate = "/go({name})"

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

I have a WebInvoke method like this;

[OperationContract]

    [WebInvoke(

        Method = "POST",
        UriTemplate = "/go",
        BodyStyle = WebMessageBodyStyle.Bare,
        RequestFormat = WebMessageFormat.Xml,
        ResponseFormat = WebMessageFormat.Xml

    )]       
    string go(string name);

and I post the data like this;

System.Net.WebClient client = new System.Net.WebClient();

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

The question is how can I take the data from posted message in go() method without using a uri template like this;

UriTemplate = "/go({name})"

Because I want to sent large amount of data and I cannot sent it in uri template

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海未深 2024-08-20 06:07:40

这是解决方案;

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

Here is the solution;

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

and HTTP POST request is;

POST /go Modify HTTPS/1.1
Content-Type: application/json; charset=utf-8
Host: WebserviceURL
Content-Length: length

{"name":"someName"}
淡看悲欢离合 2024-08-20 06:07:40

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

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

(这里可能有错误的方法,但它存在于请求对象的某个地方)
// 仅.NET 4.0

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

Do you have access to System.Web.HttpContext.Current?

You could try System.Web.HttpContext.Current.Request.Form, or, it looks like you are using XML for your request format, so you may want to try loading an XElement or XmlDocument from the request stream.

(may have the wrong method here, but it exists somewhere in the request object)
// .NET 4.0 only

XElement el = XElement.Load(HttpContext.Current.Request.GetRequestStream());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文