gpt4 book ai didi

java - 服务器返回 HTTP 响应代码 : 400 for URL : HttpURLConnection Error

转载 作者:行者123 更新时间:2023-12-01 13:26:28 31 4
gpt4 key购买 nike

我正在尝试在 java 中复制以下 unix cURL 命令:

curl -X POST http://api.nigelsmall.com/xml-cypher -d @test/files/abba.xml

编辑:

更具体地说,我正在尝试在 java 中复制以下 cURL 命令:

curl -X POST http://api.nigelsmall.com/xml-cypher -d "<?xml version='1.0' encoding='UTF-8' ?><group><member></member></group>"

到目前为止,我有以下代码:

String urlString = "http://api.nigelsmall.com/xml-cypher";
try {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "application/xml");
conn.setDoOutput(true);
conn.setRequestMethod("POST");
String data = "<?xml version='1.0' encoding='UTF-8' ?><group><member></member></group>";
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
out.write(data);
out.flush();


BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
out.close();
rd.close();
}


catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}

但是 java.io.IOException:服务器返回 HTTP 响应代码:400 对于 URL:http://api.nigelsmall.com/xml-cypher发生。谁能找出问题所在吗?

我唯一能想到的是,我可能需要将 XML 放入文件中并将其作为请求发送。但我不知道该怎么做。有人能帮我解决这个问题吗?提前致谢。

最佳答案

这不是你的错。 Web 服务的文档缺少一个重要的细节:

如果发布 JSON,您可以这样做。发布 XML 时,您必须提供 XML,就像以 HTML 表单发布一样:application/x-www-form-urlencoded

source code您可以看到的服务:

def _convert(method):
a = request.accept_mimetypes.best_match(["application/json", "text/html"])
if a == "application/json":
xml = request.get_data().decode("utf-8")
else:
xml = request.form["xml"] # <-- xml is a form field!!
# ...

我已经尝试过 DavidWebb这只是 HttpURLConnection 的一个小包装,它起作用了:

public void testNigelsmall() throws Exception {

String payload = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" +
"<group id=\"abba\">\n" +
" <member id=\"agnetha\">\n" +
" <name>Agnetha Fältskog</name>\n" +
" <birth date=\"1950-04-05\" />\n" +
" </member>\n" +
" <member id=\"frida\">\n" +
" <name>Anni-Frid Lyngstad</name>\n" +
" <birth date=\"1945-11-15\" />\n" +
" </member>\n" +
" <song id=\"waterloo\" release_date=\"1974-03-04\">\n" +
" <name>Waterloo</name>\n" +
" <length min=\"2\" sec=\"42\" />\n" +
" </song>\n" +
"</group>";

Webb webb = Webb.create();
Response<String> response = webb
.post("http://api.nigelsmall.com/xml-cypher")
.param("xml", payload)
.asString();

assertEquals(200, response.getStatusCode());
assertNotNull(response.getBody());
assertTrue(response.getBody().startsWith("CREATE"));
assertTrue(response.getBody().contains("(abba)-[:member]->(frida)"));
}

输出:

CREATE
(abba),
(agnetha {name:"Agnetha F\u00e4ltskog",`birth date`:"1950-04-05"}),
(frida {name:"Anni-Frid Lyngstad",`birth date`:"1945-11-15"}),
(waterloo {`length sec`:42,`length min`:2,name:"Waterloo"}),
(abba)-[:member]->(agnetha),
(abba)-[:member]->(frida),
(abba)-[:song {release_date:"1974-03-04"}]->(waterloo)

关于java - 服务器返回 HTTP 响应代码 : 400 for URL : HttpURLConnection Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21792201/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com