我想调用此curl命令以从JSON中的Java获取申请人姓名列表
curl -u uname:pass my_REST_Endpoint_provided_by_vendor
这是我的代码:
URL myURL = new URL("url"); HttpURLConnection conn = (HttpURLConnection) myURL.openConnection(); conn.setRequestProperty("Content-Type", "application/json"); conn.setDoOutput(true); conn.setRequestMethod("PUT"); String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(unamepass.getBytes("UTF-8")); conn.setRequestProperty ("Authorization", basicAuth); int code = conn.getResponseCode(); // 200 = HTTP_OK System.out.println("Response (Code):" + code); System.out.println("Response (Message):" + conn.getResponseMessage());
如果我在命令提示符下运行此命令,它将正常运行并提供输出,但是如果运行此代码,我将得到 Response (Code):405 java.io.IOException: Server returned HTTP response code: 405 for URL:
我要去哪里错了?
之所以出现405错误,是因为您使用的是HTTP方法PUT而不是GET(默认情况下,curl使用GET)。删除行:
conn.setRequestMethod("PUT");
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句