如何读取HttpRequest的主体发布到REST Web服务 – C#WCF

如何读取HttpRequest的主体发布到REST Web服务 – C#WCF,第1张

概述我有一个呼叫REST Web服务的Apex类( SalesForce中的一个类). public class WebServiceCallout { @future (callout=true) public static void sendNotification(String aStr) { HttpRequest req = new HttpReq 我有一个呼叫REST Web服务的Apex类( SalesForce中的一个类).

public class WebServiceCallout {    @future (callout=true)    public static voID sendNotification(String aStr)     {        httpRequest req = new httpRequest();        httpResponse res = new httpResponse();        http http = new http();        req.setEndpoint('http://xx.xxx.xxx.xx:41000/TestService/web/test');        req.setMethod('POST');        req.setheader('Content-Type','application/Json');        req.setbody(aStr); // I want to read this in the web service        try         {            res = http.send(req);        }         catch(System.CalloutException e)         {            System.deBUG('Callout error: '+ e);            System.deBUG(res.toString());        }    }}

REST Web服务(C#,WCF)如下所示:

public interface ITestService{    [OperationContract]    [WebInvoke(Method = "POST",ResponseFormat = Webmessageformat.Json,BodyStyle = WebMessageBodyStyle.bare,UriTemplate = "/test")]    string Test(string aStr);}

test()方法执行基本 *** 作.

我跑的时候

WebServiceCallout.sendNotification("a test message")

POST到达Web服务但是如何读取在sendNotification()方法中设置的httpRequest req主体中设置的内容 – req.setbody(aStr);?

也就是说,字符串Test的参数应该是什么(字符串aStr);是?

我是否需要在WebInvoke或App.config(例如绑定)中指定其他任何配置/属性?

解决方法 如果要读取传入请求的原始主体,则应将参数类型定义为Stream,而不是字符串.下面的代码显示了实现场景的一种方法,而 http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx的帖子提供了有关此“原始”模式的更多信息.

public class StackOverflow_25377059{    [ServiceContract]    public interface ITestService    {        [OperationContract]        [WebInvoke(Method = "POST",UriTemplate = "/test")]        string Test(Stream body);    }    public class Service : ITestService    {        public string Test(Stream body)        {            return new StreamReader(body).ReadToEnd();        }    }    class RawMapper : WebContentTypeMapper    {        public overrIDe WebContentFormat GetmessageformatForContentType(string ContentType)        {            return WebContentFormat.Raw;        }    }    public static voID test()    {        var baseAddress = "http://" + Environment.Machinename + ":8000/Service";        var host = new ServiceHost(typeof(Service),new Uri(baseAddress));        var binding = new WebhttpBinding { ContentTypeMapper = new RawMapper() };        host.AddServiceEndpoint(typeof(ITestService),binding,"").Behaviors.Add(new WebhttpBehavior());        host.open();        Console.Writeline("Host opened");        var req = (httpWebRequest)httpWebRequest.Create(baseAddress + "/test");        req.Method = "POST";        req.ContentType = "application/Json";        var reqStream = req.GetRequestStream();        var body = "a test message";        var bodyBytes = new UTF8EnCoding(false).GetBytes(body);        reqStream.Write(bodyBytes,bodyBytes.Length);        reqStream.Close();        var resp = (httpWebResponse)req.GetResponse();        Console.Writeline("http/{0} {1} {2}",resp.ProtocolVersion,(int)resp.StatusCode,resp.StatusDescription);        foreach (var header in resp.headers.AllKeys)        {            Console.Writeline("{0}: {1}",header,resp.headers[header]);        }        Console.Writeline();        Console.Writeline(new StreamReader(resp.GetResponseStream()).ReadToEnd());        Console.Writeline();    }}

顺便说一下,你的传入请求在技术上是不正确的 – 你说(通过Content-Type)你发送的是JsON,但请求体(测试消息)不是有效的JsON字符串(它应该用引号括起来 – “测试消息”改为JsON字符串).

总结

以上是内存溢出为你收集整理的如何读取HttpRequest的主体发布到REST Web服务 – C#WCF全部内容,希望文章能够帮你解决如何读取HttpRequest的主体发布到REST Web服务 – C#WCF所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1065321.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存