开发者

call wcf service by HttpWebRequest

开发者 https://www.devze.com 2023-04-06 11:48 出处:网络
When I have that service: [OperationContract] ResponseMessage GetData(RequestMessage message); Where class RequestMessage

When I have that service:

[OperationContract]
ResponseMessage GetData(RequestMessage message);

Where

class RequestMessage
{
  public string data    
}

class ResponseMessage
{
  public string data
}

and call this service

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开发者_如何学Python);
  requestStream.Write(bytes, 0, bytes.Length);
  requestStream.Close();
}

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

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 :/


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"}.

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();
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号