WCF Rest JSON服务400错误请求

问题描述 投票:0回答:1

所以我写了我的第一个WCF项目,似乎它使用我的浏览器和jquery工作,但后来我写了一个客户端和事情搞砸了一点......实际上我用该客户端做的一切似乎导致了400个错误的请求响应。 ..所以我读了一些帖子,发现一个很好的解决方法是使用小提琴手,并开始挑选...

由于提琴手无法识别我的客户,我已经使用我的客户端将数据直接发送到它... https://docs.google.com/file/d/0ByOtHJSZT_GtNHZqTVZMdVVqZEU/edit?usp=sharing你可以看到一个截图在这里。

因为我可以看到oly不同的东西是一些标题的漏洞(这对我来说似乎没什么用)和一个使用内容类型作为application / jsonp其他text / html(我认为是主要问题) 。 糟糕的是我在发送请求之前设置了内容类型标头,但没有结果,请注意在右侧面板中您仍然可以看到application / json。 我很困惑。

    private void SendSelectedFile()
    {
        string url = "http://" + WindowsFormsApplication1.Properties.Settings.Default.HostAddress + "/Service1.svc/rest/PostFileToServer";
        string jsonMsg = "{\"fileContentAsBase64String\":\"" + this.textBox1.Text + "\",\"where\":\"D:\\Temp.dwg\"}";
        byte[] buffer = System.Text.Encoding.UTF8.GetBytes(jsonMsg);

        HttpWebRequest wr = WebRequest.Create(new Uri(url)) as HttpWebRequest;
        wr.Method = "POST";
        wr.ContentType = "application/json; charset=utf-8";
        wr.ContentLength = buffer.Length;
        //wr.TransferEncoding = "UTF-8";
        System.IO.Stream rs = wr.GetRequestStream();
        rs.Write(buffer , 0, buffer.Length);
        rs.Close();

        WebResponse response = wr.GetResponse();
    }

这是服务的接口

    [WebInvoke(
        Method = "POST",
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "/PostFileToServer"
    )]
    Boolean PostFileToServer(string fileContentAsBase64String, string where);
c# .net wcf bad-request
1个回答
2
投票

我测试了你的代码,然后我将400缩小到传递给你的where成员的值:

string jsonMsg = "{\"fileContentAsBase64String\":\"" + this.textBox1.Text + "\",\"where\":\"D:\\Temp.dwg\"}";

我猜你正试图传递值D:\\Tempdwg ,但反斜杠\\ T似乎被服务器解释为转义序列。 尝试base64编码该值,或双重转义它\\\\\\\\

WCF跟踪显示它在字符“T”周围反序列化JSON字符串时出错

© www.soinside.com 2019 - 2024. All rights reserved.