通过 HttpWebRequest 调用 wcf 服务 [英] call wcf service by HttpWebRequest

查看:81
本文介绍了通过 HttpWebRequest 调用 wcf 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我有那个服务时:

[OperationContract]
ResponseMessage GetData(RequestMessage message);

哪里

class RequestMessage
{
  public string data    
}

class ResponseMessage
{
  public string data
}

并调用此服务

string data2 = ""
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/Service.svc/GetData");
request.ContentType = "application/json";
request.Method = "POST";
request.KeepAlive = true;

using (Stream requestStream = request.GetRequestStream())
{
  var bytes = Encoding.UTF8.GetBytes(data2);
  requestStream.Write(bytes, 0, bytes.Length);
  requestStream.Close();
}

var response = (HttpWebResponse)request.GetResponse();
var abc = new StreamReader(response.GetResponseStream()).ReadToEnd();

作为 data2,我应该发送字符串mydata"还是应该将其包装为 json 格式:{"message": {"data":"mydata"}}

as data2 should I send string "mydata" or should I wrap it in json format : {"message": {"data":"mydata"}}

??我在理解如何通过邮寄方式在客户端发送数据以在服务端正确获取数据时遇到问题:/

?? I have problem with understand how should be send data on client side by post to get it properly on service side :/

推荐答案

您没有提到服务是如何定义的.假设您的端点使用 webHttpBinding,并且端点行为具有默认值 <webHttp/>,那么 body 样式的默认值是Bare",这意味着请求应该只包含参数的序列化版本.对于这种情况,您可以发送字符串 {"data":"hello world"}.

You didn't mention how the service is defined. Assuming your endpoint uses webHttpBinding, and an endpoint behavior with <webHttp/> with default values, then the default value for the body style is "Bare", which means that the request should contain only the serialized version of the parameter. For this case, you can send the string {"data":"hello world"}.

如果您想快速找到 WCF 服务的预期格式,您可以使用 WCF 客户端,使用相同的合同/绑定/行为,并向服务器发送消息(并在提琴手上捕获它).例如,下面的代码显示了一个类似于您的服务器,以及一个向其发送请求的客户端.

If you want a quick way to find what's the expected format for a WCF service, you can use a WCF client, using the same contract / binding / behaviors, and send a message to the server (and capture it on fiddler). For example, the code below shows a server similar to yours, and a client which sends a request to it.

public class StackOverflow_7492678
{
    public class RequestMessage
    {
        public string data;
    }
    public class ResponseMessage
    {
        public string data;
    }
    [ServiceContract]
    public interface ITest
    {
        [OperationContract]
        ResponseMessage GetData(RequestMessage message);
    }
    public class Service : ITest
    {
        public ResponseMessage GetData(RequestMessage message)
        {
            return new ResponseMessage { data = message.data };
        }
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        var endpoint = host.AddServiceEndpoint(typeof(ITest), new WebHttpBinding(), "");
        endpoint.Behaviors.Add(new WebHttpBehavior());
        host.Open();
        Console.WriteLine("Host opened");

        ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new WebHttpBinding(), new EndpointAddress(baseAddress));
        factory.Endpoint.Behaviors.Add(new WebHttpBehavior());
        ITest proxy = factory.CreateChannel();

        Console.WriteLine(proxy.GetData(new RequestMessage { data = "mydata" }).data);

        ((IClientChannel)proxy).Close();
        factory.Close();

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}

这篇关于通过 HttpWebRequest 调用 wcf 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆