gpt4 book ai didi

c# - 提取 HTTP POST 数据 (WCF C#)

转载 作者:可可西里 更新时间:2023-11-01 16:31:14 26 4
gpt4 key购买 nike

我如何获取在我的 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();

但是当我在 WCF 中收到 POST 请求时,我找不到使用 WebOperationContext.Current.IncomingRequest 提取它的方法,如何从 HTTP POST 请求中提取数据?

最佳答案

我的猜测是您正在使用 WCF Rest 服务并且可以提取 GET 参数但无法读取 RAW 发布数据?

如果是这种情况,则将 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;
}

有关详细信息,请参阅此链接: http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx

关于c# - 提取 HTTP POST 数据 (WCF C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5447715/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com