Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# httpResponse短一个或两个字符_C#_Wcf_Web Services_Httpresponse - Fatal编程技术网

C# httpResponse短一个或两个字符

C# httpResponse短一个或两个字符,c#,wcf,web-services,httpresponse,C#,Wcf,Web Services,Httpresponse,我正在写一个wcf网络服务。主要关注json和xml,但我需要在两种情况下返回字符串 我正在设置测试: [OperationContract] [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "test")] string BasicString(); 我自己发送

我正在写一个wcf网络服务。主要关注json和xml,但我需要在两种情况下返回字符串

我正在设置测试:

 [OperationContract]
 [WebInvoke(Method = "GET",
      ResponseFormat = WebMessageFormat.Json,
      BodyStyle = WebMessageBodyStyle.Bare,
      UriTemplate = "test")]
  string BasicString();
我自己发送字符串,绕过标准的json返回,从而欺骗响应

   protected string BasicString()
   {
       string text = "ok";
       HttpResponse response = HttpContext.Current.Response;
       //response.AddHeader("Content-Length", "");
       response.ContentType = "text/html";
       //response.BufferOutput = true;
       response.Output.Write(text);
       //response.ContentEncoding = System.Text.Encoding.UTF8;
       response.End();
       return string.Empty;
   }
大多数浏览器的返回值都是“OK”。然而,在谷歌chrome和其他一些浏览器上,它使用最后一个字符。上面写着。。。“O”。我正在本地IIS上运行它

如果我把它放在托管的IIS上,它会返回“OK”,有时还会返回329编码错误

在所有情况下,我需要从哪里开始寻找它以返回“OK”

标题?我已经尝试禁用上面的一些行,或者已经禁用了不同的值

HTTP嗅探器为我提供了以下信息: 请求:

GET/Service/Test.svc/string HTTP/1.1主机:本地主机连接: 保持活力接受: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8 用户代理:Mozilla/5.0(Windows NT 6.2)AppleWebKit/537.36(KHTML, 如Gecko)Chrome/27.0.1453.110 Safari/537.36接受编码: gzip,deflate,sdch接受语言:en-US,en;q=0.8

答复:

HTTP/1.1 200 OK缓存控制:专用传输编码:分块 内容类型:text/html;charset=utf-8内容编码:gzip变化: 接受编码服务器:Microsoft IIS/8.0 X-AspNet-Version:4.0.30319 X-Powered-By:ASP.NET日期:Sun,2013年6月16日12:19:00 GMT

七十 ��������<代码>I�%&/M�{J�J��T��$ؐ@������iG#)�*��eVe]f@�흼��{���{���;�不���?\fdl��J�ɞ!���?~|?"���


卡洛斯的评论引出了答案

使用IO.stream作为返回类型,而不是字符串

[ServiceContract] public class RawService {
    [OperationContract, WebGet]
    public System.IO.Stream GetValue()
    {
        string result = "Hello world";
        byte[] resultBytes = Encoding.UTF8.GetBytes(result);
        return new MemoryStream(resultBytes);
    } }

如果打开web developer tools,那么请求是什么样子的,你能发布web developer tools上显示的实际请求/响应对象吗?用请求信息更新了帖子。响应看起来“很”混乱。“我在欺骗响应。”-请不要。如果您使用的是WCF,则ASP.NET管道中会有一些用于该框架的模块。如果您希望返回“原始”(非JSON、非XML)字符串,请使用。对于您的最后一条评论,响应不是(必要的)搞砸了,它是gzip编码的,这就是为什么你不能理解它。HTTP中的文本是UTF-8,Visual Studio默认使用UTF-16。你的“ok”响应——当被视为UTF-8时……等同于字母“o”和空终止符(值为零)。某些浏览器只使用状态代码值并打印自己的描述,但是--如果您要尝试处理响应,请记住编码是相关的--不仅作为标题值,而且--您生成的文本也必须正确编码。示例正在写入输出流already——问题是它正在编写UTF-16编码的字符串,而不是UTF-8编码的字符串,就像您的nice代码那样。