我想在java中複製以下UNIX cURL命令:服務器返回的HTTP響應代碼:400網址:HttpURLConnection的錯誤
捲曲-X POST http://api.nigelsmall.com/xml-cypher -d @測試/文件/ abba.xml
編輯:
更具體地講,我試圖複製在Java中,下列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網址:發生http://api.nigelsmall.com/xml-cypher。任何人都可以找到問題所在?
我能想到的唯一可能是我需要把我的XML放在一個文件中,並將它作爲請求發送。但我不知道該怎麼做。任何人都可以幫助我嗎?提前致謝。
我會設置一個日誌代理(請參閱http://stackoverflow.com/questio ns/4939382/logging-post-data-from-request-body)並逐字節比較請求。 – user3159253
發送該XML到該URL會產生一個'400'響應,無論它是否由'curl'或其他發送。我對這個問題是什麼感到困惑。你的意思是你想能夠讀取錯誤響應體? –
如果我在unix上執行以下命令 curl -X POST http://api.nigelsmall.com/xml-cypher -d「<?xml version ='1.0'encoding ='UTF-8'?> 「 我以」創建「的形式返回響應。如果你去鏈接「http://api.nigelsmall.com/xml-cypher並點擊按鈕轉換爲密碼創建語句,你會明白我指的是什麼.. –
Ganesh