通过WCF中的HTTP Post接受表单字段

时间:2021-10-18 06:31:39

I need to accept form data to a WCF-based service. Here's the interface:

我需要接受表单数据到基于WCF的服务。这是界面:

[OperationContract]
[WebInvoke(UriTemplate = "lead/inff",
    BodyStyle = WebMessageBodyStyle.WrappedRequest)]
int Inff(Stream input); 

Here's the implementation (sample - no error handling and other safeguards):

这是实现(示例 - 没有错误处理和其他安全措施):

public int Inff(Stream input)
{

    StreamReader sr = new StreamReader(input);
    string s = sr.ReadToEnd();
    sr.Dispose();

    NameValueCollection qs = HttpUtility.ParseQueryString(s);
    Debug.WriteLine(qs["field1"]);
    Debug.WriteLine(qs["field2"]);

    return 0;
}

Assuming WCF, is there a better way to accomplish this besides parsing the incoming stream?

假设WCF,除了解析传入流之外,还有更好的方法来实现吗?

2 个解决方案

#1


5  

I remember speaking to you about this at DevLink.

我记得在DevLink上和你谈过这件事。

Since you have to support form fields the mechanics of getting those (what you are currently doing) don't change.

由于您必须支持表单字段,因此获取这些(您当前正在执行的操作)的机制不会发生变化。

Something that might be helpful, especially if you want to reuse your service for new applications that don't require the form fields is to create a channel that deconstructs your stream and repackages it to XML/JSON/SOAP/Whatever and have your form clients communicate with the service through that while clients that don't use forms can use another channel stack. Just an idea...

可能有用的东西,特别是如果你想为不需要表单字段的新应用程序重用你的服务,就是创建一个解构你的流并将它重新打包成XML / JSON / SOAP / Whatever并拥有你的表单客户端的通道通过这种方式与服务进行通信,而不使用表单的客户端可以使用另一个通道堆栈。只是一个想法......

Hope that helps. If you need help with the channel feel free to let me know.

希望有所帮助。如果您需要频道帮助,请随时告诉我。

#2


0  

You can serialize your form fields with jquery and package it as json request to wcf service.

您可以使用jquery序列化表单字段,并将其作为json请求打包到wcf服务。

#1


5  

I remember speaking to you about this at DevLink.

我记得在DevLink上和你谈过这件事。

Since you have to support form fields the mechanics of getting those (what you are currently doing) don't change.

由于您必须支持表单字段,因此获取这些(您当前正在执行的操作)的机制不会发生变化。

Something that might be helpful, especially if you want to reuse your service for new applications that don't require the form fields is to create a channel that deconstructs your stream and repackages it to XML/JSON/SOAP/Whatever and have your form clients communicate with the service through that while clients that don't use forms can use another channel stack. Just an idea...

可能有用的东西,特别是如果你想为不需要表单字段的新应用程序重用你的服务,就是创建一个解构你的流并将它重新打包成XML / JSON / SOAP / Whatever并拥有你的表单客户端的通道通过这种方式与服务进行通信,而不使用表单的客户端可以使用另一个通道堆栈。只是一个想法......

Hope that helps. If you need help with the channel feel free to let me know.

希望有所帮助。如果您需要频道帮助,请随时告诉我。

#2


0  

You can serialize your form fields with jquery and package it as json request to wcf service.

您可以使用jquery序列化表单字段,并将其作为json请求打包到wcf服务。