我使用 cURL 呼叫下面的 URL
curl 'http://username:[email protected]/merlin/Login.cgi'
我得到了適當的回應
{ "Session" : "0xb3e01810", "result" : { "message" : "OK", "num" : 200 } }
我從瀏覽器(直接打開鏈接)、郵遞員甚至使用 NodeJs 得到相同的回應,
但是當我在 java 中使用 okhttp 向這個 url 發送 GET_Request 時得到回應代碼 401
我的 okhttp 的 java 代碼是
Request request = new Request.Builder()
.url("http://username:[email protected]/merlin/Login.cgi")
.build();
try {
com.squareup.okhttp.Response response = client.newCall(request).execute();
System.out.println(response.code());
} catch (IOException e) {
e.printStackTrace();
}
uj5u.com熱心網友回復:
使用基本身份驗證。url 中包含用戶名和密碼,以 分隔:,與基本身份驗證相同。我測驗了,沒問題。我的片段是:
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("http://192.168.1.108/merlin/Login.cgi")
.header("Authorization", "Basic " Base64.getEncoder().encodeToString("username:password".getBytes()))
.get().build();
try (Response response = client.newCall(request).execute()) {
assert response.body() != null;
String res = response.body().string();
} catch (Exception e) {
e.printStackTrace();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/481569.html
上一篇:為什么此請求無法使用此url
下一篇:我需要遍歷資料表的每一行
