private static CookieStore cookieStore = new BasicCookieStore();
private HttpClient client = new DefaultHttpClient();
private static HttpContext httpContext = new BasicHttpContext();
public static void main(String[] args) throws Exception {
String url = "http://www.codechef.com";
String account = "http://www.codechef.com/node?destination=node";
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
List<NameValuePair> postParams = http.getFormParams();
sendPost(url, postParams);
}
private void sendPost(String url, List<NameValuePair> postParams)
throws Exception {
HttpPost post = new HttpPost(url);
post.setHeader("Accept","text/plain");
post.setHeader("Accept-Language", "en-US");
post.setHeader("Connection", "keep-alive");
post.setEntity(new UrlEncodedFormEntity(postParams));
HttpResponse response = client.execute(post,httpContext);
List<Cookie> cook=cookieStore.getCookies();
if(cook==null)
System.out.println("Null");
else{
System.out.println(cook.size());
for(Cookie c:cook){
String cookieReader=c.getName()+" = "+c.getValue()+";domain= "+c.getDomain();
System.out.println(cookieReader);
}
}
}
public List<NameValuePair> getFormParams() throws UnsupportedEncodingException {
List<NameValuePair> paramList = new ArrayList<NameValuePair>();
paramList.add(new BasicNameValuePair("name","name"));
paramList.add(new BasicNameValuePair("pass","pass"));
return paramList;
}
After the post method, the size of the cookie is 0, but I checked from the browser, and it shows sessionID cookie. But I cannot extract it using this code to authenticate user. Thank you.
You should pay attention on the HTML form fields.
There is often hidden fields required in order to achieve the post process correctly.
Take at look the html source of the login form on this website (inspect element...)
The form fields are: "user", "pass" plus two hidden fields "form_build_id" and "form_id".
Try adding this in your 'getFormParams' function:
paramList.add(new BasicNameValuePair("form_id","user_login_block"));
Related
I try to pass parameters in post request in integration test, but I get response that required parameter "source" aint detected. Maybe you will know what is cause. Thank you.
public void testUploadBundleFromRepository() throws IOException, InterruptedException {
String boundary = "---------------"+ UUID.randomUUID().toString();
String uri = String.format("http://%s:%d/upload/", HOST, PORT);
HttpPost httpPost = new HttpPost(uri);
httpPost.setHeader(HTTP.CONTENT_TYPE, ContentType.MULTIPART_FORM_DATA.getMimeType()+";boundary="+boundary);
List<NameValuePair> postParameters = new ArrayList<>();
postParameters.add(new BasicNameValuePair("source","repo"));
postParameters.add(new BasicNameValuePair("id","1"));
postParameters.add(new BasicNameValuePair("start", "true"));
httpPost.setEntity(new UrlEncodedFormEntity(postParameters, "UTF-8"));
HttpResponse response = getHttpClient().execute(httpPost);
//assert in future
}
I think you should call setRequestBody not setEntity
httpPost.setRequestBody(postParameters);
The main idea is. I do an httpget request to take the form of login page from moodle. (html code)
Then i find the form params in the html code and i fill them.
Then i send httpPost request to the login page with the parameters (email and pass)
And in the end trying to get the html code from a new website that needed to login first.
Buts i am always getting the same code . The one from login Page.
I have tried this to gmail and i think it works. I dont get the same code after ther post request at least.
If anyone can help me it would be great becase i am in a little hurry.
Thanks in advance :)
public class MyHttpClient {
private String cookies;
private HttpClient client = getNewHttpClient() ;
private final String USER_AGENT = "Chrome/39.0.2171.65";
#SuppressLint("NewApi")
/* this method is called from the
* mainactivity .
*courseUrl is the page i want to go after i login
*/
public String getHtmlFromElearn(String courseURL) throws Exception{
String url = "https://elearn.uoc.gr/login/index.php"; //the login page
CookieHandler.setDefault(new CookieManager());
MyHttpClient http = new MyHttpClient();
String page = http.GetPageContent(url); //gets the html code of login page
List<NameValuePair> postParams =
http.getFormParams(page, "mymail#csd.uoc.gr","mypass"); //pasing the email and pass params to post request
http.sendPost(url, postParams); // sends the post request with the params found in form
String result = http.GetPageContent(courseURL);
System.out.println("Done");
return result;
}
/*
* sends post request with params to login to the website
*/
private void sendPost(String url, List<NameValuePair> postParams)
throws Exception {
//was getting exception for main thred without this
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
HttpPost post = new HttpPost(url);
// add header details
post.setHeader("Host", "elearn.uoc.gr");
post.setHeader("User-Agent", USER_AGENT);
post.setHeader("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
post.setHeader("Accept-Language", "el-GR,el;q=0.8");
post.setHeader("Cookie", getCookies());
post.setHeader("Connection", "keep-alive");
post.setHeader("Referer", "https://elearn.uoc.gr/login/index.php");
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
post.setEntity(new UrlEncodedFormEntity(postParams,HTTP.UTF_8));
HttpResponse response = client.execute(post);
// THE RESPONSE I AM GETTING IS 200
int responseCode = response.getStatusLine().getStatusCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + postParams);
System.out.println("Response Code : " + responseCode);
//copy the response page in to a string
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line).append("\n");
}
if(result.toString().contains("login failed")){ // checks if login succeeded
System.out.println("login faild");
}
System.out.println(result.toString());
}
#SuppressLint("NewApi")
/*
* gets the html code from the url given
*/
private String GetPageContent(String url) throws Exception {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
//httpget request to get the html of page
HttpGet request = new HttpGet(url);
request.setHeader("User-Agent", USER_AGENT);
request.setHeader("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
request.setHeader("Accept-Language", "el-GR,el;q=0.8");
HttpResponse response = client.execute(request);
int responseCode = response.getStatusLine().getStatusCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
//copies response html code to string
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
String result = new String();
String line = "";
while ((line = rd.readLine()) != null) {
result+=line+"\n";
}
// set cookies
setCookies(response.getFirstHeader("Set-Cookie") == null ? "" :
response.getFirstHeader("Set-Cookie").toString());
return result;
}
/*gets the email and pass and returns a lists withh all the paramas
* for post request to login
*/
public List<NameValuePair> getFormParams(
String html, String username, String password)
throws UnsupportedEncodingException {
System.out.println("Extracting form's data...");
Document doc = Jsoup.parse(html);
Element loginform = doc.getElementById("login");
Elements inputElements = loginform.getElementsByTag("input");
List<NameValuePair> paramList = new ArrayList<NameValuePair>();
for (Element inputElement : inputElements) {
String key = inputElement.attr("name");
String value = inputElement.attr("value");
if (key.equals("username")){
value = username;
}
else if (key.equals("password")){
value = password;
}
paramList.add(new BasicNameValuePair(key, URLEncoder.encode(value, "UTF-8")));
}
return paramList;
}
public String getCookies() {
return cookies;
}
public void setCookies(String cookies) {
this.cookies = cookies;
}
/*
*return an http client that trusts all cerfificates
*/
public HttpClient getNewHttpClient() {
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", sf, 443));
ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
return new DefaultHttpClient(ccm, params);
} catch (Exception e) {
return new DefaultHttpClient();
}
}
}
I used Jsoup.connect and logged in successfully to any web site
Response res = Jsoup
.connect("loginPage.com/login.php")
.data("username", "myUserName", "password", "myPass")
.method(Method.POST)
.execute();
//This will get you cookies
Map<String, String> loginCookies = res.cookies();
And then if i want to visit any other web page i use the cookies that login page returned me.
Document doc = Jsoup.connect("urlYouNeedToBeLoggedInToAccess")
.cookies(loginCookies)
.get()
I'm trying to to submit a form, but before this form I have to log in, so I should do 2 posts in a line, somebody has any idea in how to do that, there is what I have here:
// Making HTTP request
try {
String redirectedUrl = getUrl(url);
// defaultHttpClient
HttpPost httpPost = new HttpPost(redirectedUrl);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", login));
nameValuePairs.add(new BasicNameValuePair("password", password));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String html = null;
if (entity != null) {
InputStream instream = entity.getContent();
try {
html = streamToString(instream);
} finally {
instream.close();
}
}
The problem there is that when I log in, it just retrieve a response and I can't do anything with, I need to post inside the resource that is protected, somebody has any idea about what I should do. The getUrl function is shown below:
private String getUrl(String url) throws ClientProtocolException, IOException {
HttpGet httpget = new HttpGet(url);
HttpContext context = new BasicHttpContext();
HttpResponse response = httpClient.execute(httpget, context);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK)
throw new IOException(response.getStatusLine().toString());
HttpUriRequest currentReq = (HttpUriRequest) context
.getAttribute(ExecutionContext.HTTP_REQUEST);
HttpHost currentHost = (HttpHost) context
.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
return (currentReq.getURI().isAbsolute()) ? currentReq
.getURI().toString() : (currentHost.toURI() + currentReq
.getURI());
}
Help setting cookie to HttpClient
Created a program which logins to an external web service. However, to obtain vital information from
an HTTP GET, I am unable to pass in the cookie (generated from the login).
public class ClientHelper {
private final static String PROFILE_URL =
"http://externalservice/api/profile.json";
private final static String LOGIN_URL = "http://externalservice/api/login";
public static Cookie login(final String username, final String password) {
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(LOGIN_URL);
HttpContext localContext = new BasicHttpContext();
client.getParams().setParameter("http.useragent", "Custom Browser");
client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
HttpVersion.HTTP_1_1);
List<Cookie> cookies = null;
BasicClientCookie cookie = null;
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("user", username));
nameValuePairs.add(new BasicNameValuePair("passwd", password));
UrlEncodedFormEntity entity =
new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8);
entity.setContentType("application/x-www-form-urlencoded");
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post, localContext);
cookies = client.getCookieStore().getCookies();
System.out.println(cookies.get(1));
cookie = new BasicClientCookie(cookies.get(1).getName(), cookies.get(1).getValue());
cookie.setVersion(cookies.get(1).getVersion());
cookie.setDomain(cookies.get(1).getDomain());
cookie.setExpiryDate(cookies.get(1).getExpiryDate());
cookie.setPath(cookies.get(1).getPath());
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
}
catch (Throwable e) {
e.printStackTrace();
}
return cookie;
}
public static void getProfile(Cookie cookie) {
DefaultHttpClient client = new DefaultHttpClient();
HttpContext context = new BasicHttpContext();
CookieStore cookieStore = new BasicCookieStore();
cookieStore.addCookie(cookie);
client.setCookieStore(cookieStore);
context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpGet get = new HttpGet(PROFILE_URL);
HttpResponse response;
try {
response = client.execute(get, context);
BufferedReader rd =
new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
App.java (class that uses ClientHelper):
public class App {
private static final String USER = "myusername";
private static final String PASSWD = "mypassword";
public static void main(String[] args) {
Cookie cookie = ClientHelper.login(USER, PASSWD);
ClientHelper.getProfile(cookie);
}
}
When I run App, I am able to login (I see the generated JSON) but the getProfile() method returns an empty JSON object:
{}
From the command line, using curl I am trying to emulate this:
curl -b Cookie.txt http://externalservice/api/profile.json
This actually works but not my Java program.
Try by executing this part of the code:
List<Cookie> cookies = client.getCookieStore().getCookies();
for (Cookie cookie : cookies) {
singleCookie = cookie;
}
After
HttpResponse response = client.execute(post, localContext);
After changing your code to get the cookies after the login request, you actually are getting all the cookies from the request.
I suspect the problem is that whatever Cookie it is at index 1 in the CookieStore isn't the one you need, and obviously since it's not throwing an IndexOutOfBounds exception when you do that, there's at least one other Cookie in there (at index 0). Return the list of cookies and send all of them with your profile request.
Taking your code, changing all those indexes from 1 to 0 and pointing at this simple PHP script shows that it is receiving then sending the cookies:
<?php
setcookie("TestCookie", "Some value");
print_r($_COOKIE);
?>
output:
[version: 0][name: TestCookie][value: Some+value][domain: www.mydomain.org][path: /][expiry: null]
Array
(
)
Array
(
[TestCookie] => Some value
)
I figured it out... I was creating two different HTTP clients instead of using the same one.
#Brian Roach & Raunak Agarwal thank you both very much for the help!
Here's the fix:
public static HttpClient login(final String username, final String password)
{
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(LOGIN_URL);
client.getParams().setParameter("http.useragent", "Custom Browser");
client.getParams().setParameter(
CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
try
{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("user", username));
nameValuePairs.add(new BasicNameValuePair("passwd", password));
UrlEncodedFormEntity entity =
new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8);
entity.setContentType("application/x-www-form-urlencoded");
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
BufferedReader reader =
new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
}
catch (Throwable e) { e.printStackTrace(); }
return client;
}
public static void getProfile(HttpClient client)
{
HttpGet get = new HttpGet(PROFILE_URL);
HttpResponse response;
try
{
response = client.execute(get);
BufferedReader reader =
new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
}
catch (ClientProtocolException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }
}
I am having a strange problem while using Apache HttpClient in an Android app.
My app needs to Login to a website and get some data and then logout.
My current code looks like this:
public class BlueClient {
private String hostUrl;
private DefaultHttpClient client;
private HttpContext localContext;
public BlueClient(String hostUrl,DefaultHttpClient httpClient,HttpContext localContext) {
this.hostUrl = hostUrl;
this.client = httpClient;
this.localContext = localContext;
}
public boolean doLogin(String userName,String password){
String url = getHostUrl()+"do_login";//loggin will set a session cookie
HttpPost post = new HttpPost(url);
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username",userName));
nameValuePairs.add(new BasicNameValuePair("password",password));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post,localContext);
//ok now if response is ok then return true
} catch (Exception e) {
}
return false;
}
public MyStuff getMyStuff(){
String url = getHostUrl()+"/getMyStuff/"; //this url requires authentication. the sesion cookie should do that
HttpGet get = new HttpGet(url);
try {
HttpResponse response = client.execute(get,localContext);
//ok get my stuff from response and return
} catch (Exception e) {
}
return null;
}
public boolean doLogout(){
String url = getHostUrl()+"do_logout";//it clears the cookie so the session is invalidated
HttpGet get = new HttpGet(url);
try {
HttpResponse response = client.execute(get,localContext);
//ok the cookie is cleared
}
} catch (Exception e) {
}
return false;
}
}
And when i call these function i do like this. It works in emulator but not in device
HttpContext context = new BasicHttpContext();
CookieStore cookieStore = new BasicCookieStore();
context.setAttribute(ClientContext.COOKIE_STORE,cookieStore);
DefaultHttpClient httpClient = new DefaultHttpClient();
BlueClient myClient = new BlueClient("http://myHost.com/",httpClient,context);
myClient.doLogin("user","pass");
// it should've printed the cookies set by the server but i get nothing here !
D.log(context.getAttribute(ClientContext.COOKIE_STORE));
// as this is another subsequesnt request it shoud carry the cookies back to the server but as the cookies are not set this function gives me nothig :-(
myClient.getMyStuff();
myClient.doLogout();
Can anyone please shed some light on this. Why its not working in the device?
Ah I finally found the problem!
My server runs on CodeIgniter. And CodeIgniter set the expiry date of the session cookie (ci_session) as Netscape cookie draft compliant. And the format is EEE, dd-MMM-yy HH:mm:ss zzz and Default CookiePoicy used by HttpClient is RFC_2109. So when the httpClient tries to parse the cookie data it fails on parsing the expiry date. I had to explicitly set the Cookie Policy and date format.
So my final code looks like this:
BasicHttpParams params = new BasicHttpParams();
String[] dateFormats = {"EEE, dd-MMM-yy HH:mm:ss zzz"};
params.setParameter(CookieSpecPNames.DATE_PATTERNS,Arrays.asList(dateFormats));
DefaultHttpClient httpClient = new DefaultHttpClient(params);
httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.NETSCAPE);//explicitly set the cookie policy
HttpContext context = new BasicHttpContext();
CookieStore cookieStore = new BasicCookieStore();
context.setAttribute(ClientContext.COOKIE_STORE,cookieStore);
BlueClient myClient = new BlueClient("http://myHost.com/",httpClient,context);
myClient.doLogin("user","pass");
myClient.getMyStuff();//now i get my stuff !
myClient.doLogout();
Hope it saves someone's time :)