【发布时间】:2015-11-14 04:50:58
【问题描述】:
我正在作为客户端调用 API。我使用 java 来做到这一点。我现在只需要一个回应。稍后我将转换输出。 我的代码如下:
String data = "User=admin&Password=1234&Authorization=basic&Keyword=nana";
URL url;
try {
url = new URL("http://119.235.102.65/library/index.php/API/Search/basic");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
int responseCode = conn.getResponseCode();
System.out.println("Response code: " + responseCode);
boolean isSuccesResponse = responseCode < 400;
InputStream responseStream = isSuccesResponse ? conn.getInputStream() : conn.getErrorStream();
wr.close();
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
我收到错误代码 401。
my url is : http://119.235.102.65/library/index.php/API/Search/basic
my username is : admin
my password is : 1234
我还要从数据库中搜索一个关键字,即 nana。 关键字 = 娜娜。
【问题讨论】:
-
是的。它需要授权。
-
在您的代码示例中,您没有将凭据作为请求的一部分发送。这些可能需要作为标头或请求参数发送。
标签: java api rest restful-authentication restful-architecture