HttpURLConnection - 当发布Json Data时,另一侧的服务器接收带有反斜杠的字符串

时间:2017-02-16 22:03:02

标签: java json httpurlconnection

当我使用HttpURLConnection发布Json数据时,另一侧的服务器会收到带反斜杠的字符串。

setRequestProperty设置为:

conn.setRequestProperty("Content-Type","application/json");

这是我的代码

HttpURLConnection connection = null;

Gson gson = new Gson();
String jsonEnvio = gson.toJson(object);

URL url = new URL(urlSend);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(method);

if (headers != null) 
{
    for (Map.Entry<String, String> entry : headers.entrySet()) {
        connection.setRequestProperty(entry.getKey(), entry.getValue());
    }
}

connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setConnectTimeout(30000);

if (object != null) 
{
    connection.setRequestProperty("Content-Length", Integer.toString(jsonEnvio.getBytes().length));

    DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
    wr.writeBytes(jsonEnvio);
    wr.close();
}

InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));

这是变量jsonEnvio

的字符串

{"codOrdemComplexa":"12346544","codFornecedor":"56","codEtapaProcesso":"622635","codTipoElemento":"6","codTipoProjeto":"1","hash":"780fbd8103abe35899d219e8856123a6","nomeArquivo":"OrdemDeServico_12346544_Wed Feb15 18:05:25 BRST 2017","dataColetaEvidencias":"Feb 15, 2017 6:05:25 PM"}

这是另一方的服务器接收的字符串

{\"codOrdemComplexa\":\"12346544\",\"codFornecedor\":\"56\",\"codEtapaProcesso\":\"622635\",\"codTipoElemento\":\"6\",\"codTipoProjeto\":\"1\",\"hash\":\"780fbd8103abe35899d219e8856123a6\",\"nomeArquivo\":\"OrdemDeServico_12346544_Wed Feb15 18:05:25 BRST 2017\",\"dataColetaEvidencias\":\"Feb 15, 2017 6:05:25 PM\"}

另一方的服务器回应这不是一个有效的Json。

我需要帮助才能找出需要修改的内容来解决这个问题。

使用JDK 1.8.0_121

0 个答案:

没有答案