HTTP requests with basic authentication

前端 未结 4 1736
悲&欢浪女
悲&欢浪女 2020-12-07 23:10

I have to download and parse XML files from http server with HTTP Basic authentication. Now I\'m doing it this way:

URL url = new URL(\"http         


        
4条回答
  •  天命终不由人
    2020-12-07 23:52

    • DefaultHttpClient deprecated
    • addHeader must have 2 parameters

    Updated code block using HttpClient 4.5.2

    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpGet httpGet = new HttpGet("https://test.com/abc.xyz");
    httpGet.addHeader("Authorization", BasicScheme.authenticate(new UsernamePasswordCredentials("login", "password"), "UTF-8"));
    
    HttpResponse httpResponse = httpClient.execute(httpGet);
    HttpEntity responseEntity = httpResponse.getEntity();
    

提交回复
热议问题