如何设置httpRequest中的header Authentication



如何在httpRequest中设置header认证?

我试过了,但是效果不太好。

 connection.setRequestMethod("GET");
 connection.setRequestProperty ("Authorization", accesToken);
 connection.setRequestProperty("Accept", "application/json");

以下是我使用基本身份验证对HTTP请求所做的操作:

String pageUrl ="http://......";
String username = "yourUsername";
String password = "yourPassword";
HttpParams httpParams = new BasicHttpParams();
int timeoutConnection = 10000;
HttpConnectionParams.setConnectionTimeout(httpParams, timeoutConnection);
int timeoutSocket = 10000;
HttpConnectionParams.setSoTimeout(httpParams, timeoutSocket);
HttpClient httpClient = new DefaultHttpClient(httpParams);
HttpGet httpGet = new HttpGet(pageUrl);
String credential = Base64.encodeToString( (username+":"+password).getBytes("UTF-8"), Base64.DEFAULT);
httpGet.setHeader("Authorization", "Basic " + credential.substring(0, credential.length()-1));
httpGet.setHeader("Accept", "application/json");
httpGet.setHeader("Connection", "close");
                            
HttpResponse httpResponse = httpClient.execute(httpGet);
StatusLine statusLine = httpResponse.getStatusLine();               
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    httpResponse.getEntity().writeTo(outputStream);
    String responseString = outputStream.toString();
    System.out.println(responseString);
    // ......  // proceeding operations             
}

最新更新