How to load data from HTTPPOST request in server side?

1.2k views Asked by At

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

2

There are 2 answers

1
EEE On BEST ANSWER

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"}
1
LorenVS On

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());