Java HttpClient访问具有会话ID的图像

时间:2018-05-03 03:45:10

标签: java http authentication httpclient

我正在尝试通过网址访问图片。问题是我必须通过会话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);

1 个答案:

答案 0 :(得分:0)

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

中提取的图片

enter image description here

这样你必须这样做:

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", ""));