ITPub博客

首页 > IT职业 > IT职场 > 用HTTP/Post与WCF互操作

用HTTP/Post与WCF互操作

IT职场 作者:rainbows 时间:2021-09-09 09:14:38 0 删除 编辑

WCF does not support raw HTTP requests with the content-type 'application/x-www-form-urlencoded' (which is the content-type the browser sends when you hit 'submit' in a

element) out of the box unless you treat the request body as a Stream.

This makes sense if you consider that the same principle applies when returning data, as you can see here in the Picture Services sample, both the GetDocumentation() and GetPicture() operations return a Stream with data; all you need to do is set the content-type properly and you're good to go.

I've created a little sample that shows how to POST data from a HTML form and parse it in WCF with a little help from the System.Web namespace.

Consider the following service contract:

[ServiceContract]
public interface ISampleService
{
    [OperationContract]
    [WebInvoke(UriTemplate = "invoke")]
    void DoWork(Stream input);
}


HTML source:


    :      

    :      

Here's how you read the input Stream:public void DoWork(Stream input) {     StreamReader sr = new StreamReader(input);     string s = sr.ReadToEnd();     sr.Dispose();       NameValueCollection qs = HttpUtility.ParseQueryString(s);       string firstName = qs["firstName"];     string lastName = qs["lastName"];       // Do work here }

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2442/viewspace-2801053/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-15

  • 博文量
    112
  • 访问量
    1268