当呼叫简单获取请求使用curl和Java Okhttp时,我得到了不同的结果

发布于 2025-01-31 13:53:59 字数 945 浏览 2 评论 0原文

我使用curl在下面拨打下面的URL

curl 'http://username:[email protected]/merlin/Login.cgi'

,并且得到适当的响应,

{ "Session" : "0xb3e01810", "result" : { "message" : "OK", "num" : 200 } }

直接打开链接),邮递员甚至Nodejs获得相同的

响应

我会从浏览器( Okhttp是

    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();
    }

I call below URL using cURL

curl 'http://username:[email protected]/merlin/Login.cgi'

and I get proper response

{ "Session" : "0xb3e01810", "result" : { "message" : "OK", "num" : 200 } }

I get the same response from browser (directly open link), postman and even with NodeJs,

but I get response code 401 when send GET_Request to this url using okhttp in java

my java code with okhttp is

    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();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终难遇 2025-02-07 13:53:59

使用基本身份验证。在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();
        }

Use basic authentication. Having username and password in the url, separated with :, is the same as basic authentication. I tested and it's ok. my snippet is:

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();
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文