WCF服务始终在响应正文中返回随机数

时间:2016-08-02 21:14:49

标签: java c# rest wcf

我有一个用C#编写的WCF服务:

namespace MyServices
{
    [ServiceContract]
    public interface IMyService
    {
        [OperationContract]
        [WebGet(UriTemplate = "/foo/{param}")]
        string foo(string param);
    }
}

public class MyService : IMyService
{
    public string foo(string param)
    {
        //Do stuff with param
        return "Some processed data";
    }
}

每当我使用这个服务时,它工作正常,除了响应中的每一行总是前面有一个数字(有时是一个带字符的数字)。最后一行总是0。

下面是一个使用Java编写的服务的客户端:

public class Main
{
    public static void main(String[] args)
    {
        try 
        {
            Socket soc = new Socket(InetAddress.getByName("<SOME IP ADDRESS>"), 80);
            PrintWriter out = new PrintWriter(soc.getOutputStream());
            String headers = "GET /MyService.svc/foo/some_data HTTP/1.1\r\n"
                             + "Host: <SOME HOST>\r\n"
                             + "Content-Type: text/plain;charset=utf-8\r\n"
                             + "Content-Length: 0\r\n\r\n";

            out.print(headers);
            out.flush();

            BufferedReader in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
            String response;

            while((response = in.readLine())!=null)
            {
                //Do something with the response            
                System.out.println(response);
            }
            out.close();
            soc.close();
        catch (UnknownHostException e) 
        {
            System.err.println(e.getMessage());
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
}

这将输出以下内容:

HTTP/1.1 200 OK
Cache-Control: private
Transfer-Encoding: chunked
Content-Type: text/plain;charset=utf-8
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Set-Cookie: ARRAffinity=2e5dd814a4cb78f8a5825d56c5879cd18fa384d20597b10f3c685ffe2cff1f53;Path=/;Domain=<SOME DOMAIN NAME>
Date: Tue, 02 Aug 2016 22:53:25 GMT

15
"Some processed data"
0

起初我认为数字表示下一行中的字符数,0表示邮件正文的结尾,但似乎并非如此。

感谢任何帮助,谢谢:)

1 个答案:

答案 0 :(得分:1)

这是因为Transfer-Encoding被分块(https://en.wikipedia.org/wiki/Chunked_transfer_encoding)。 15是HEX,意思是这个块中有21个字符,0表示结束标记。