HTTP POST格式WCF RESTful服务 [英] Http Post Format for WCF Restful Service

查看:175
本文介绍了HTTP POST格式WCF RESTful服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,超级新手的问题。考虑下面的WCF功能:

Hey, super newbie question. Consider the following WCF function:

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class Service1
{
     private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

     [WebInvoke(UriTemplate = "", 
                Method = "POST", 
                ResponseFormat = WebMessageFormat.Json,
                RequestFormat = WebMessageFormat.Json,
                BodyStyle = WebMessageBodyStyle.Bare) ]
     public SomeObject DoPost(string someText)
     {
          ...
          return someObject;

在提琴手什么将我的请求头和身体是什么样子?

In fiddler what would my request headers and body look like?

感谢您的帮助。

推荐答案

我已经略有改变WCF服务有一个更好的例子, 。写了一个样本测试程序(见下文)

I've slightly changed your WCF service to have a better example and written a sample test program (see below).

第一个测试执行的URL的 HTTP GET请求://本地主机:57211 / Service1.svc / getcar / 1 的。在 1 的结尾是一个参数。端口号可能是你的情况有所不同。其结果是:

The first test executes a GET request for the URL http://localhost:57211/Service1.svc/getcar/1. The 1 at the end is a parameter. The port number may be different in your case. The result is:

{"ID":1,"Make":"Porsche"}

第二次测试通过发送相同的数据(除了法拉利保时捷)的URL执行POST请求的的http://本地主机:57211 / Service1.svc / updatecar / 1 的。其结果是:

The second test executes a POST request by sending the same data (except Ferrari for Porsche) to the URL http://localhost:57211/Service1.svc/updatecar/1. The result is:

{"ID":1,"Make":"Ferrari"}

这要求具有在URL中既有参数( 1 的)加上请求数据(JSON结构)作为第二个参数,作为请求体发送

This request has both a parameter in the URL (the 1) plus the request data (a JSON structure) as a second parameter, transmitted as the request body.

通过网络调试器,它看起来像这样(简体):

With a network debugger, it would look like this (simplified):

POST /Service1.svc/updatecar/1 HTTP/1.1
Host: localhost:57211
Content-Type: application/json
Content-Length: 25

{"ID":1,"Make":"Ferrari"}

HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Sat, 25 Dec 2010 19:16:19 GMT
Content-Length: 25
Content-Type: application/json; charset=utf-8

{"ID":1,"Make":"Ferrari"}

我希望帮助

TestService.cs:

class TestService
{
    static void Main(string[] args)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:57211/Service1.svc/getcar/1");
        WebResponse response = request.GetResponse();
        string result = new StreamReader(response.GetResponseStream()).ReadToEnd();
        Console.WriteLine(result);

        string requestData = "{\"ID\":1,\"Make\":\"Ferrari\"}";
        byte[] data = Encoding.UTF8.GetBytes(requestData);
        request = (HttpWebRequest)WebRequest.Create("http://localhost:57211/Service1.svc/updatecar/1");
        request.Method = "POST";
        request.ContentType = "application/json";
        Stream dataStream = request.GetRequestStream();
        dataStream.Write(data, 0, data.Length);
        dataStream.Close();

        response = request.GetResponse();
        result = new StreamReader(response.GetResponseStream()).ReadToEnd();
        Console.WriteLine(result);
    }
}



IService.cs:

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Bare,
        UriTemplate = "/getcar/{id}")]
    Car GetCar(string id);

    [OperationContract]
    [WebInvoke(RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Bare,
        UriTemplate = "/updatecar/{id}")]
    Car UpdateCar(string id, Car car);
}


[DataContract]
public class Car
{
    [DataMember]
    public int ID { get; set; }

    [DataMember]
    public string Make { get; set; }
}



Service.svc:

public class Service1 : IService1
{
    public Car GetCar(string id)
    {
        return new Car { ID = int.Parse(id), Make = "Porsche" };
    }


    public Car UpdateCar(string f, Car car)
    {
        return car;
    }
}



Service1.svc(标记):

<%@ ServiceHost Language="C#" Debug="true" Service="JSONService.Service1" CodeBehind="Service1.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

这篇关于HTTP POST格式WCF RESTful服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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