Accepting form fields via HTTP Post in WCF

拈花ヽ惹草 提交于 2019-12-07 00:21:10

问题


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

[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?


回答1:


I remember speaking to you about this at 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...

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




回答2:


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



来源:https://stackoverflow.com/questions/147328/accepting-form-fields-via-http-post-in-wcf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!