如何在我的WCF服务中收到的HTTP POST请求中获取数据?
我使用HTTP POST从另一个服务发送数据:
string ReportText = "Hello world";
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();
Run Code Online (Sandbox Code Playgroud)
但是当我在WCF中收到POST请求时,我找不到使用WebOperationContext.Current.IncomingRequest提取它的方法,如何从HTTP POST请求中提取数据?
我的猜测是你正在使用WCF Rest服务,你可以提取GET参数,但是你无法读取RAW后期数据?
如果是这种情况,则将Stream参数添加到Contract声明中参数列表的末尾.如果函数末尾有单个流,则框架将其视为原始数据流.
[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;
}
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅此链接:http: //blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx
归档时间: |
|
查看次数: |
8798 次 |
最近记录: |