extract HTTP POST data (WCF C#)
How do i get the data inside an HTTP POST request , that is received in my WCF Service?
i send the data from another service using HTTP POST:
string ReportText = "Hello world";
开发者_如何学JAVA ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(ReportText);
// Prepare web request...
String serverURL = ConfigurationManager.AppSettings["REPORT"];
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serverURL);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
// Send the data.
newStream.Write(data, 0, data.Length);
newStream.Close();
but when i receive the POST request in the WCF i can't find a way to extract it using WebOperationContext.Current.IncomingRequest, how do i extract the data from the HTTP POST request ?
My guess is that you are using a WCF Rest service and you can pull the GET parameters but you are unable to read RAW post data?
If this is the case then add a Stream parameter to the end of the parameter list in the Contract declaration. If there is a single stream at the end of the function, the framework treats it as a raw data stream.
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "DoSomething?siteId={siteId}&configTarget={configTarget}",
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
bool DoSomething(string itemid, Stream body);
public bool DoSomething(int siteId, string configTarget, Stream postData)
{
string data = new StreamReader(postData).ReadToEnd();
return data.Length > 0;
}
See this link for more details: http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx
Hello world
isn't exactly application/x-www-form-urlencoded
. You need to encode the post message body accordingly someproperty=Hello%20world
as well as use WCF HTTP bindings.
This isn't a WCF question, as you're not using WCF. What you're really doing is submitting a form using HTTP Post, and you need to make a web page to receive and handle that. You can do that with the Request.Form collection.
Here's a simple example: http://bytes.com/topic/asp-net/answers/655226-how-use-request-form
精彩评论