【问题标题】:Java HttpClient accessing image with session idJava HttpClient 使用会话 ID 访问图像
【发布时间】:2018-05-03 03:45:09
【问题描述】:

我正在尝试通过 url 访问图像。问题是我必须通过会话 ID 进行身份验证才能访问图像。我正在尝试通过身份验证,但我得到了 403。另外,在通过身份验证后如何添加会话 ID 和其他信息以获取图像的输入流?这是我的尝试:

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("https://cloud.openalpr.com/account/login/");
List<NameValuePair> form = new ArrayList<>();
form.add(new BasicNameValuePair("username","user"));
form.add(new BasicNameValuePair("password", "password"));

UrlEncodedFormEntity entity = new UrlEncodedFormEntity(form, Consts.UTF_8);
    httpPost.setEntity(entity);
    httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
httpPost.setHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X ...");
CloseableHttpResponse response = httpClient.execute(httpPost);

【问题讨论】:

    标签: java http authentication httpclient


    【解决方案1】:

    此服务器需要其他字段:csrfmiddlewaretoken 和 next。请参阅下面从网站https://cloud.openalpr.com/account/login/提取的图片

    你必须这样做:

    List<NameValuePair> form = new ArrayList<>();
    form.add(new BasicNameValuePair("username","user"));
    form.add(new BasicNameValuePair("password", "password"));
    form.add(new BasicNameValuePair("csrfmiddlewaretoken", "xxxxxxx"));
    form.add(new BasicNameValuePair("next", ""));
    

    【讨论】:

    • 嗯,我知道您是否知道如何生成 csrf 中间件令牌?
    • 我建议在开始此申请之前进行一次爬网。在客户端和服务器端生成相同的值的可能性太小了。
    • 是否可以从表单中获取 csrf 令牌?
    • 是的!一种方法是仅使用正则表达式执行第一个请求以获取 csrf 值。继续使用您的代码后
    猜你喜欢
    • 2011-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-08
    • 1970-01-01
    • 1970-01-01
    • 2011-04-14
    • 2017-10-17
    相关资源
    最近更新 更多