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

时间:2009-11-25 12:36:05

标签: c# wcf post uri webinvoke

我有像这样的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模板发送它

2 个答案:

答案 0 :(得分: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"}

答案 1 :(得分:0)

您是否可以访问System.Web.HttpContext.Current

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

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

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