Java HttpClient problem with Cookies - java

I use Apache HttpClient to first Request a page for the Cookies, and then post to a page with those Cookies. To be able to get the second page, the Cookie must be sent with the post. I've read somewhere that HttpClient automatically saves and Sends the needed Cookies, but somehow I keep stuck at the first page, probably due to Cookies not being get properly, or not being sent properly.
public class Main {
static BufferedWriter writer;
public static void main(String args[]) throws ClientProtocolException, IOException {
getRequest();
}
public static void getRequest() throws ClientProtocolException, IOException {
HttpClient client = new DefaultHttpClient();
//the request to get the Cookies
HttpGet request = new HttpGet("http://www.SiteNameCutOut.cz");
List <NameValuePair> parameters = new ArrayList <NameValuePair>();
parameters.add(new BasicNameValuePair("view_state", "eaftOTAPef3NDs79"));
parameters.add(new BasicNameValuePair("age", "23"));
parameters.add(new BasicNameValuePair("button", "go"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters);
HttpPost post = new HttpPost("http://www.SameSiteAsAbove.cz");
post.setEntity(entity);
//post.addHeader(request.getFirstHeader("Set-Cookie")); maybe?
post.addHeader("Host","theSiteHost");
post.addHeader("User-Agent","Mozilla/5.0 (Windows NT 6.0; rv:2.0.1) Gecko/20100101 Firefox/4.0.1");
post.addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
post.addHeader("Accept-Language","en-us,en;q=0.5");
post.addHeader("Accept-Encoding","gzip, deflate");
post.addHeader("Accept-Charset","ISO-8859-1,utf-8;q=0.7,*;q=0.7");
post.addHeader("Keep-Alive","115");
post.addHeader("Connection","keep-alive");
client.execute(request);
try {
request.abort();
HttpResponse response = client.execute(post);
writer = new BufferedWriter(new FileWriter("test001.html"));
writer.write(HttpHelper.request(response)); //gets html of the response
} catch (IOException ex) {
System.out.println("**Error**");
} finally {
if(writer != null){
writer.close();
}
else{
System.out.println("Writer is null");
}
}
}
}
So i hope anyone can help me, Thanks !

You should indicate how to manage the cookies like this:
request.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);

Related

Java 8: Apache HttpClient failing authentication

I am attempting to use Apache HttpClient API to access Atlassian Confluence wiki pages.
Here is my code:
public class ConcfluenceTest{
public static void main(String[] args) {
String pageID = "107544635";
String hostName = "valid_hostname";
String hostScheme = "https";
String username = "verified_username";
String password = "verified_password";
int port = 443;
//set up the username/password authentication
BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(hostName, port, AuthScope.ANY_REALM, hostScheme),
new UsernamePasswordCredentials(username, password));
HttpClient client = HttpClientBuilder.create()
.setDefaultCredentialsProvider(credsProvider)
.build();
try {
HttpGet getRequest = new HttpGet("valid_url");
System.out.println(getRequest.toString());
HttpResponse response = client.execute(getRequest);
//Parse the response
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
} catch (UnsupportedEncodingException e) {
System.out.println(e.getStackTrace());
} catch (IOException e) {
System.out.println(e.getStackTrace());
}
}
}
When I attempt to execute this code, the printed response is the HTML of the Log In screen, which means that the authentication failed. This code does, however, return the correct response when I provide it with the URL to a page that is not restricted to registered users (i.e credentials aren't required). I also tried all permutations of port/scheme.
Can someone tell me what I am missing?
Afaik, if http-basic-auth is supported, something like
user:password#server:port/path
should work, too. You could see if that works with a browser.
If Confluence dosen't support basic auth, use firebug to find out the action of the login-form (eg. the path, something like /dologin.action), the method (POST) and the Names of the user/password fields.
With that information you can create a request like this:
HttpPost httpPost = new HttpPost(fullFormActionUrlWithServerAndPort);
List <NameValuePair> nvp = new ArrayList <NameValuePair>();
nvp.add(new BasicNameValuePair("name-of-the-user-field", "your-user-name"));
nvp.add(new BasicNameValuePair("name-of-the-pass-field", "your-password"));
httpPost.setEntity(new UrlEncodedFormEntity(nvp));

apache httpcomponents.httpClient strangest error ever

we are developing an android app and my current part is to implement uploading files.
I'm using httpcore-4.2.2, httpclient-4.2.2 and httpmime-4.2.2.
My problem now is that the file will only be send with the httpclient if the URI has a really specific description... if I use another URI the post-body on the server-side is empty... and I am really not capable of understanding this issue.
My code looks as follows:
public static String getUploadLink() {
String server = Mobile4dApplication.getContext().getString(R.string.default_server_address);
String parameter = "/Files/upload";
String finalQuery = String.format("%s%s", server, parameter);
Log.d("UploadService", finalQuery );
return finalQuery;
}
private String uploadFile(String uploadURL, File uploadFile, String title, long id) throws UnsupportedEncodingException, FileNotFoundException {
MultipartEntity multipartEntity = new MultipartEntity( );
multipartEntity.addPart( "file", new FileBody( uploadFile ) );
HttpResult httpResult;
try {
httpResult = HttpCommunicator.sendPOST( uploadURL, multipartEntity );
} catch (IOException e) {
e.printStackTrace();
return null;
}
// TODO remove sysout line when it is not needed anymore
System.out.println("result: " + httpResult.getResultMessage());
return httpResult.getResultMessage();
}
public static HttpResult sendPOST(String targetURL, HttpEntity entity) throws IOException {
HttpClient httpClient = getHttpClient();
HttpParams httpParams = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, connectionTimeoutMillis);
HttpConnectionParams.setSoTimeout(httpParams, socketTimeoutMillis);
HttpPost httppost = new HttpPost(targetURL);
httppost.setEntity(entity);
HttpResponse response = httpClient.execute(httppost);
String result = "";
if (response != null && response.getEntity() != null) {
result = EntityUtils.toString(response.getEntity(), UTF_8_ENCODING);
}
return new HttpResult(result, response.getStatusLine().getStatusCode());
}
if I use the URI serveraddress/Files/upload everything looks fine on the server side. But if I change the part with "Files/upload" even in the slightest the whole post-body turns out to be empty when it reaches our server...
I am completely out of ideas... I really hope that someone can help...

How should client send json data to PHP

I have used php for server side and my client(A java program) sends a post request with json data as parameter. I am able to receive the data but the jsonData is no decoding. I am sending a valid JSON.
Below is my Client program.
public class ExampleHttpPost
{
public static void main(String args[]) throws ClientProtocolException, IOException
{
HttpPost httppost = new HttpPost("http://localhost/hello.php");
List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
try {
parameters.add(new BasicNameValuePair("data", (new JSONObject("{\"imei\":\"imei1\"}")).toString()));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
httppost.setEntity(new UrlEncodedFormEntity(parameters));
HttpClient httpclient = new DefaultHttpClient();
HttpResponse httpResponse = httpclient.execute(httppost);
HttpEntity resEntity = httpResponse.getEntity();
// Get the HTTP Status Code
int statusCode = httpResponse.getStatusLine().getStatusCode();
// Get the contents of the response
InputStream input = resEntity.getContent();
String responseBody = IOUtils.toString(input);
input.close();
// Print the response code and message body
System.out.println("HTTP Status Code: "+statusCode);
System.out.println(responseBody);
}
}
And my hello.php
<?php
$data = $_POST['data'];
var_dump($data);
$obj = json_decode($data);
if($obj==NULL){
echo "Decoding error";
}
echo $obj['imei'];
?>
Output :
HTTP Status Code: 200
string(20) "{\"imei\":\"imei1\"}"
Decoding error
It seems like your Java Application is adding slashes to the string or as suggested in the comments the PHP app is probably adding slashes to the quotes to avoid SQL injection
Try if you can get it to work by adding
$data = stripslashes($data);
Above the json_decode part

Handling HttpClient Redirects

I'm POSTing some data to a server that is answering a 302 Moved Temporarily.
I want HttpClient to follow the redirect and automatically GET the new location, as I believe it's the default behaviour of HttpClient. However, I'm getting an exception and not following the redirect :(
Here's the relevant piece of code, any ideas will be appreciated:
HttpParams httpParams = new BasicHttpParams();
HttpClientParams.setRedirecting(httpParams, true);
SchemeRegistry schemeRegistry = registerFactories();
ClientConnectionManager clientConnectionManager = new ThreadSafeClientConnManager(httpParams, schemeRegistry);
HttpClient httpClient = new DefaultHttpClient(clientConnectionManager, httpParams)
HttpPost postRequest = new HttpPost(url);
postRequest.setHeader(HTTP.CONTENT_TYPE, contentType);
postRequest.setHeader(ACCEPT, contentType);
if (requestBodyString != null) {
postRequest.setEntity(new StringEntity(requestBodyString));
}
return httpClient.execute(postRequest, responseHandler);
For HttpClient 4.3:
HttpClient instance = HttpClientBuilder.create()
.setRedirectStrategy(new LaxRedirectStrategy()).build();
For HttpClient 4.2:
DefaultHttpClient client = new DefaultHttpClient();
client.setRedirectStrategy(new LaxRedirectStrategy());
For HttpClient < 4.2:
DefaultHttpClient client = new DefaultHttpClient();
client.setRedirectStrategy(new DefaultRedirectStrategy() {
/** Redirectable methods. */
private String[] REDIRECT_METHODS = new String[] {
HttpGet.METHOD_NAME, HttpPost.METHOD_NAME, HttpHead.METHOD_NAME
};
#Override
protected boolean isRedirectable(String method) {
for (String m : REDIRECT_METHODS) {
if (m.equalsIgnoreCase(method)) {
return true;
}
}
return false;
}
});
The default behaviour of HttpClient is compliant with the requirements of the HTTP specification (RFC 2616)
10.3.3 302 Found
...
If the 302 status code is received in response to a request other
than GET or HEAD, the user agent MUST NOT automatically redirect the
request unless it can be confirmed by the user, since this might
change the conditions under which the request was issued.
You can override the default behaviour of HttpClient by sub-classing DefaultRedirectStrategy and overriding its #isRedirected() method.
It seem http redirect is disable by default. I try to enable, it work but I'm still got error with my problem. But we still can handle redirection pragmatically. I think your problem can solve:
So old code:
AndroidHttpClient httpClient = AndroidHttpClient.newInstance("User-Agent");
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
long contentSize = httpResponse.getEntity().getContentLength();
This code will return contentSize = -1 if http redirect happend
And then I handle redirect by myself after trying enable default follow redirection
AndroidHttpClient client;
HttpGet httpGet;
HttpResponse response;
HttpHeader httpHeader;
private void handleHTTPRedirect(String url) throws IOException {
if (client != null)
client.close();
client = AndroidHttpClient.newInstance("User-Agent");
httpGet = new HttpGet(Network.encodeUrl(url));
response = client.execute(httpGet);
httpHeader = response.getHeaders("Location");
while (httpHeader.length > 0) {
client.close();
client = AndroidHttpClient.newInstance("User-Agent");
httpGet = new HttpGet(Network.encodeUrl(httpHeader[0].getValue()));
response = client.execute(httpGet);
httpHeader = response.getHeaders("Location");
}
}
In use
handleHTTPRedirect(url);
long contentSize = httpResponse.getEntity().getContentLength();
Thanks
Nguyen
My solution is using HttClient. I had to send the response back to the caller. This is my solution
CloseableHttpClient httpClient = HttpClients.custom()
.setRedirectStrategy(new LaxRedirectStrategy())
.build();
//this reads the input stream from POST
ServletInputStream str = request.getInputStream();
HttpPost httpPost = new HttpPost(path);
HttpEntity postParams = new InputStreamEntity(str);
httpPost.setEntity(postParams);
HttpResponse httpResponse = null ;
int responseCode = -1 ;
StringBuffer response = new StringBuffer();
try {
httpResponse = httpClient.execute(httpPost);
responseCode = httpResponse.getStatusLine().getStatusCode();
logger.info("POST Response Status:: {} for file {} ", responseCode, request.getQueryString());
//return httpResponse ;
BufferedReader reader = new BufferedReader(new InputStreamReader(
httpResponse.getEntity().getContent()));
String inputLine;
while ((inputLine = reader.readLine()) != null) {
response.append(inputLine);
}
reader.close();
logger.info(" Final Complete Response {} " + response.toString());
httpClient.close();
} catch (Exception e) {
logger.error("Exception ", e);
} finally {
IOUtils.closeQuietly(httpClient);
}
// Return the response back to caller
return new ResponseEntity<String>(response.toString(), HttpStatus.ACCEPTED);
For HttpClient v5, just use the below:
httpClient = HttpClientBuilder.create()
.setRedirectStrategy(new DefaultRedirectStrategy() {
#Override
public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context)
throws ProtocolException {
return false;
}
}).build();

Basic HTTP Authentication on Android Phones to Rails Server

I'm trying to connect to a Rails Application Server that requires authentication. I am using the Jakarta HTTP Client for Java on a Desktop application and it works 100%. But when the exact same code is executed on the Android Emulator I get an IOException.
Here is the code, and if anyone could help me figure out why it throws the IOException that would be greatly appreciated!
private boolean login()
{
String username, password;
DefaultHttpClient client;
AuthScope scope;
Credentials myCredentials;
CredentialsProvider provider;
HttpEntity entity;
String line;
BufferedReader reader;
InputStream instream;
//Declare & Create the HTTP Client
client = new DefaultHttpClient();
//Create our AuthScope
scope = new AuthScope("10.19.9.33", 3000);
username = "admin"
password = "pass"
//Set Credentials
myCredentials = new UsernamePasswordCredentials( username, password );
//Set Provider
provider = new BasicCredentialsProvider();
provider.setCredentials(scope, myCredentials);
//Set Credentials
client.setCredentialsProvider( provider );
String url = "http://10.19.9.33:3000/users/show/2";
HttpGet get;
//Tell where to get
get = new HttpGet( url );
HttpResponse response;
try
{
response = client.execute( get );
entity = response.getEntity();
/* Check to see if it exists */
if( entity != null )
{
instream = entity.getContent();
try {
reader = new BufferedReader(new InputStreamReader(instream));
line = reader.readLine();
if( line.equals( "HTTP Basic: Access denied.") )
return false;
while ( line != null )
{
// do something useful with the response
System.out.println(line);
line = reader.readLine();
}
return true;
}
catch (IOException ex)
{
// In case of an IOException the connection will be released
// back to the connection manager automatically
throw ex;
}
catch (RuntimeException ex)
{
// In case of an unexpected exception you may want to abort
// the HTTP request in order to shut down the underlying
// connection and release it back to the connection manager.
get.abort();
throw ex;
}
finally
{
// Closing the input stream will trigger connection release
instream.close();
}
}
}
catch( ClientProtocolException cp_ex )
{
}
catch( IOException io_ex )
{
}
return false;
}
The reason it kept triggering the IOException was because the Manifest file didn't give the Application rights to the internet
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
I'm using HttpPost for this kind of task, and never had any problem:
[...]
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost(LOGIN_SERVLET_URI);
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("userName", userName));
params.add(new BasicNameValuePair("password", password));
UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
httppost.setEntity(p_entity);
HttpResponse response = client.execute(httppost);
HttpEntity responseEntity = response.getEntity();
[...]
maybe this helps you out

Categories

Resources