So i wrote my first WCF project, seems like its working using my browser and jquery, but then i wrote a client and things messed up a bit... actually seems like everything i do with that client result in a 400 bad request response... so i've read some posts and found out a good way to sort things up is using fiddler, and started picking around...

since fiddler can't identify my client, i've used my client to send the data directly to it... https://docs.google.com/file/d/0ByOtHJSZT_GtNHZqTVZMdVVqZEU/edit?usp=sharing you can see a screenshot here.

as i can see oly things that differs are the leack of some headers (that don't seems like really usefull to me) and one use content-type as application/jsonp the other text/html (wich i think is the main problem). The bad thing is i've setted the content-type header before sending the request, but with no result, please notice that in the right panel you can still see application/json. I'm getting confused.

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

and this is the interface of the service

    [WebInvoke(
        Method = "POST",
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "/PostFileToServer"
    )]
    Boolean PostFileToServer(string fileContentAsBase64String, string where);
有帮助吗?

解决方案

I tested your code, and I narrowed the 400 down to the value that's being passed to your where member:

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

I'm guessing you're trying to pass the value D:\Tempdwg, but the backslash \T seems to be getting interpreted by the server as an escape sequence. Try base64 encoding that value, or double escaping it \\\\

WCF Trace shows that it had an error deserializing the JSON string, right around the character "T"

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top