I want to access my web site that first requires a basic authorization, and then post the parameters (search keyword) to my home page.
My code can pass the basic authorization and will get the response text, however I cannot get the search result but get the home page code instead.
As such, I printed the parameters.size() and it return 0, so I doubt the HttpClient redirect method does not pass the parameters after basic authorization.
Following my code, which is trying to get the search result:
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("search_keyword", "test"));
params.add(new BasicNameValuePair("search_size", "50"));
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.setRedirectStrategy(new DefaultRedirectStrategy() {
public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
boolean isRedirect=false;
try {
isRedirect = super.isRedirected(request, response, context);
} catch (ProtocolException e) {
throw e;
}
if (!isRedirect) {
int responseCode = response.getStatusLine().getStatusCode();
if (responseCode == 301 || responseCode == 302) {
return true;
}
}
return isRedirect;
}
});
String encoding = Base64Encoder.encode("admin:password");
HttpPost httppost = new HttpPost("http://127.0.0.1/MySystem/home");
httppost.setHeader("Authorization", "Basic " + encoding);
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
//Execute and get the response.
HttpResponse response = httpclient.execute(httppost);
int responseCode = response.getStatusLine().getStatusCode();
switch(responseCode) {
case HttpURLConnection.HTTP_OK:
HttpEntity entity = response.getEntity();
List<NameValuePair> parameters = new ArrayList<NameValuePair>(URLEncodedUtils.parse(entity));
System.out.println("parameters size: "+parameters.size());
for(NameValuePair p:parameters)
System.out.println("parameter: "+ p.getName() + p.getValue());
if(entity != null) {
responseText = EntityUtils.toString(entity);
System.out.println("responseText: ");
System.out.println(responseText);
}
break;
default:
throw new Exception("Failed to pass the basic authorization. Response code: "+responseCode);
}
Updated:
The following condition will return true:
if (responseCode == 301 || responseCode == 302) {
return true;
}
Ok, I found a workaround. Basically, although 302 will redirect me to another page, the basic authorization session is passed in the current httpclient
.
The solution is to reuse the httpclient under the case HttpURLConnection.HTTP_OK:
and also close the entity bufferreader in order to prevent the "connection still allocated" Exception.
Here is the sample code:
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
responseText = sb.toString();
....
HttpPost httppost2 = new HttpPost("http://127.0.0.1/MySystem/home");
httppost2.setHeader("Authorization", "Basic " + encoding);
httppost2.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse response2 = httpclient.execute(httppost2);
HttpEntity entity2 = response2.getEntity();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.