从c#向webservice发送特殊字符时出错

时间:2013-04-27 07:06:49

标签: c# web-services post

使用POST方法将字符串发送到REST服务。

 //--------------------------Method Post--------------------------
    public static string methodPost(string header,string url,string body)
    {

        var request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "POST";
        if(header!=null) request.Headers.Add(header);
        request.ContentType = "application/json";
        byte[] byteArray = Encoding.UTF8.GetBytes(body);
        request.ContentLength = byteArray.Length;

        Stream dataStream = request.GetRequestStream();

        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();
        var response = request.GetResponse();

        Stream stream2 = response.GetResponseStream();
        StreamReader reader2 = new StreamReader(stream2);
        String ok = reader2.ReadToEnd();
        return ok;
    }

如果string body =“test send string”:success,但是string body =“test send'string”:不成功。 怎么解决?谢谢!

1 个答案:

答案 0 :(得分:0)

"test send ' string"是合法的json,但您可以尝试"test send \' string"。如果这不起作用,您需要询问管理Web服务的人员。