I am creating a 3rd party java application (desktop) that needs to connect to a php-based site and log in to gather pertinent data. There is no accessible web service, no API, and every user will have their own secure login. The site uses dojo (if that matters), and I am using Java HttpClient to send the post.
HttpPost httppost = new HttpPost("https://thewebsite.net/index/login"); // .php ?
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
//initialize the response string
String nextpage = "";
try {
// Add nvps
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("", ""));
nameValuePairs.add(new BasicNameValuePair("login", "USER"));
nameValuePairs.add(new BasicNameValuePair("", ""));
nameValuePairs.add(new BasicNameValuePair("pass", "PASSWORD"));
nameValuePairs.add(new BasicNameValuePair("Submit", ""));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
userID = EntityUtils.toString(response.getEntity());
System.out.println(nextpage);
httppost.releaseConnection();
}
...
Now, the issue I'm having is that the response given to me is a validation jscript for the user / pass fields through dojo.
<script type='text/javascript'>
dojo.require("dojox.validate._base");
function validate_RepeatPassword(val, constraints)
{
var isValid = false;
if(constraints) {
var otherInput = dijit.byId(constraints[0]);
if(otherInput) {
var otherValue = otherInput.value;
isValid = (val == otherValue);
}
}
return isValid;
}
</script>
I simply want to connect, parse an html response, and close the connection.
When I use firebug, I get this as the post method, but I can't seem to get it to run:
Referer https://thewebsite.net/index/login
Source login=USER&pass=PASSWORD
When I use the HttpPost client to construct a direct post url without namevaluepairs:
HttpPost httppost = new HttpPost("https://thewebsite.net/index/login?login=USER&pass=PASSWORD");
, I get an error response that states "the user and pass fields cannot be left blank."
My question is: Is there a direct method to log in that is simpler that I'm missing that will allow me to successfully continue past log in?
Thanks - I love the SO community; hope you can help.
I think best library for doing this is jsoup
Connection.Response res =
Jsoup.connect("https://thewebsite.net/index/login?login=USER&pass=PASSWORD")
.method(Method.POST)
.execute();
After this you need to make verification also. You need to read cookies, request parameters and header parameters and this will work.
I didn't end up using your exact code (with the post parameters), but JSoup was the fix.
here's what I used:
`res = Jsoup.connect("https://thewebsite.net/index/login")
.data("login", User).data("pass", Pass)
.userAgent("Chrome").method(Method.POST).execute();
//then I grabbed the cookie and sent the next post for the data
Document t = res.parse(); //for later use
SessionID = res.cookie("UNIQUE_NAME");
//the JSON
Connection.Response driverx = Jsoup.connect("https://thewebsite.net/datarequest/data").cookie("UNIQUE_NAME",SessionID).userAgent("Chrome").method(Method.POST).execute();`
Related
I want to read two numbers (randomly generated) from a website which are then used in order to compute a result and then submit the result using a POST request. To do so, I will also need to submit the cookie of that session so that the system is aware of the random numbers which have been produced in that particular session.
In order to read the numbers I am using Jsoup:
Document document = Jsoup.parse(Jsoup.connect("http://website.com/getNumbers").get().select("strong").text());
String[] numbers = document.text().split(" ");
String answer = methodThatComputesTheDesiredOutput(numbers);
Now I want to send a POST request that includes answer and cookies of that session. Here's a partially implemented POST request that includes only the one parameter (answer):
HttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://website.com/submitAnswer");
List<NameValuePair> params = new ArrayList<NameValuePair>(1);
params.add(new BasicNameValuePair("answer", answer);
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
How can I obtain the cookie when reading the document and then use it as a parameter of the POST request?
Extract cookies using jsoup in the following way:
Response response = Jsoup.connect("http://website.com/getNumbers").execute();
Map<String, String> cookies = response.cookies();
Document document = Jsoup.parse(response.body());
Create a BasicCookieStore using the cookies extracted using jsoup. Create a HttpContext containing the cookie store and pass it while executing your next request.
BasicCookieStore cookieStore = new BasicCookieStore();
for (Entry<String, String> cookieEntry : cookies.entrySet()) {
BasicClientCookie cookie = new BasicClientCookie(cookieEntry.getKey(), cookieEntry.getValue());
cookie.setDomain(".example.com");
cookie.setPath("/");
cookieStore.addCookie(cookie);
}
HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
HttpPost httpPost = new HttpPost("http://website.com/submitAnswer");
List<NameValuePair> params = new ArrayList<NameValuePair>(1);
params.add(new BasicNameValuePair("answer", answer);
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
httpClient.execute(httpPost, localContext);
Send your first request like below :-
Response res = Jsoup.connect("login Site URL")
.method(Method.GET)
.execute();
Now get the cookies and send new request with the cookies something like below :-
CookieStore cookieStore = new BasicCookieStore();
for (String key : cookies.keySet()) {
Cookie cookie = new Cookie(key, cookies.get(key));
cookieStore.addCookie((org.apache.http.cookie.Cookie) cookie);
}
HttpClientContext context = HttpClientContext.create();
context.setCookieStore(cookieStore);
HttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://website.com/submitAnswer");
httpClient.execute(httpPost,context);
I am trying to create new issue at bibucket, but i don't know how to work with http. I try many things but it stil dosen't work. This is one of my attempts:
URL url = new URL("https://api.bitbucket.org/1.0/repositories/"
+ accountname + "/" + repo_slug + "/issues/"
+ "?title=test&content=testtest");
HttpsURLConnection request = (HttpsURLConnection) url.openConnection();
request.setRequestMethod("POST");
consumer.sign(request);
request.connect();
I don't have a problem with GET requests. But here I don't know how to send parametrs and sign the message.
Here is documentation of API
https://confluence.atlassian.com/display/BITBUCKET/issues+Resource#issuesResource-POSTanewissue
How to do this properly?
In the end I figured this out. The parametrs isn't part of the URL, but if you use stream, You can't sign it.
The solution is use Apache HttpComponents library and add parameters like in code below:
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("https://api.bitbucket.org/1.0/repositories/"
+ accountname + "/" + repo_slug + "/issues/");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("title", "test"));
nvps.add(new BasicNameValuePair("content", "testtest"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
consumer.sign(httpPost);
HttpResponse response2 = httpclient.execute(httpPost);
try {
System.out.println(response2.getStatusLine());
HttpEntity entity2 = response2.getEntity();
// do something useful with the response body
// and ensure it is fully consumed
EntityUtils.consume(entity2);
} finally {
httpPost.releaseConnection();
}
}
But you must use CommonsHttpOAuthConsumer which is in special signpost library for commonshttp.
I have seen you've already solved but here it says that you need to authenticate with OAuth and in the page you linked that you need to authenticate in order to create new issues.
It also links to this page for OAuth implementations for many languages. I am going to post it for knowledge.
I've researched all the internet and found articles on How to use http post from android to php server. I'm not looking for examples of how to pass data from android to website. I'm looking for reasons why POST variable is always NULL in PHP. Is there some setting I need to check for on PHP (I've checked the output buffering, max post, etc) or import a class for the HTTP POST to work correctly for POST variable.
The below codes basically insert values into mysql database. Because the POST variables are blank, it's inserting blank rows. This verified that it's connecting to my server/database, but the POST values are not being passed. Any idea? =)
Below are the codes:
//Java Code-------------------------------------------------
public static void executeHttpGet(Context context, String urlName){
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
String postURL = G.GetUrl(urlName);
HttpPost httpPost = new HttpPost(postURL);
httpPost.setHeader("Accept", "text/html,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
try {
nameValuePairs.add(new BasicNameValuePair("value1", "My Name"));
nameValuePairs.add(new BasicNameValuePair("value2", "My Address"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httpPost);
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
}
//PHP code ----------------------------------------------------
connectMySQL();
mysql_query("
INSERT INTO myhome(myname,myaddress,createdate)
VALUES('{$_POST['value1']}','{$_POST['value2']}',NOW())
;")
or die(mysql_error());
Can you try to print $_POST['value1'] and $_POST['value2'] before using them in your SQL and see what they print?
For the sake of simplicity I would suggest you rewrite your PHP code:
connectMySQL();
$value1 = trim($_POST['value1']);
$value2 = trim($_POST['value2']);
if ($value1 != "" && $value2 != "") {
mysql_query("
INSERT INTO myhome(myname,myaddress,createdate)
VALUES('".$value1."','".$value2."',NOW())
;")
}
Also, once you have verified it is working, I highly recommend you read about SQL injection attacks and try to use bind variables or parameterized queries instead of directly using request parameter values in your SQL statement.
I am trying to implement a class that gets data from mysql via my web service. I have previously used a http post to get information from a table but this time I intend a user to input a string into an editText, press search and the textview to display the query.For example, Imagine there are two columns of the mysql table: Firstname and surname; I would like to be able to get the surname by searching the Firstname (Entering the Firstname into the EditText and displaying the surname of that person).I have developed the PHP script but is it possible to use the HTTP get method based on an input string? how? I've only seen tutorials directing straight to the php link
This is a example how you could do it by using NameValuePairs which you can pass to the php file using a post request.
private void sendData(ArrayList<NameValuePair> data)
{
// 1) Connect via HTTP. 2) Encode data. 3) Send data.
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost("http://www.blah.com/AddAccelerationData.php");
httppost.setEntity(new UrlEncodedFormEntity(data));
HttpResponse response = httpclient.execute(httppost);
Log.i("postData", response.getStatusLine().toString());
//Could do something better with response.
}
catch(Exception e)
{
Log.e("log_tag", "Error: "+e.toString());
}
}
Now lets say you want to use this method to pass info(i.e. your parameter to the php file.
//Add data to be send.
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("parameter",editTextValue));
this.sendData(nameValuePairs);
Now on the php side of things you can then get this parameter value by calling:
//Retrieve the data.
$parameter = $_POST['parameter'];
//Now call on your query or function or w/e it is using this parameter.
To use GET, simply encode the values into your URL, e.g.
String url = "http://myserver.net/script.php?first=" + URLEncoder.encode(first) +
"&last=" + URLEncoder.encode(last);
And then use an HttpGet object with your HttpClient:
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(new HttpGet(url));
Processing the response is then the same as if you had posted it.
I'm new to http programming and I'm attempting to authenticate against a website that doesn't use an API and I'm having some trouble. I found a few other questions that seemed to be similar to mine, but none had an answer that worked for me.
I've tried several different approaches but haven't found one that works yet. Actually, I had it work for me once, but didn't check in that code (I know - what was I thinking?). I haven't been able to get back to that working version again. Here are a few things that I've tried so far:
HttpClient client = new DefaultHttpClient(); //or any method to get a client instance, with a 'threadsafe' connection manager or otherwise
Credentials credentials = new UsernamePasswordCredentials(userName, passwd);
((DefaultHttpClient) client).getCredentialsProvider()
.setCredentials(AuthScope.ANY, credentials);
// website is defined as my target website & this constitutes the valid login URL
HttpPost post = new HttpPost(https + website + "/login");
HttpEntity entity = new StringEntity("Username="+ userName +"&Password="+ passwd);
post.setEntity(entity);
HttpResponse response = client.execute(post);
EntityUtils.consume(entity);
entity = response.getEntity();
EntityUtils.consume(entity);
// the protected part of the site is over http after authentication succeeds
HttpGet get = new HttpGet(http + website +"/protected");
response = client.execute(get);
entity = response.getEntity();
String content = EntityUtils.toString(entity);
At this point the 'entity' I get back from the website is "error":"Unauthorized Access."
You'll notice that I have a 'Credentials' instance being passed to the HttpClient and I'm also putting the user name & password in the HttpPost entity. I tried each of these approaches separately and they both returned the "error":"Unauthorized Access." result.
I've tried the DefaultHttpClient, which uses a single thread connection manager, as well as 'ThreadSafeClientConnManager' and neither worked.
first try to login using this url (copy the url to textpad or something first - then edit it):
https://www.centraldispatch.com/login?uri=%2Fprotected%2F&Username=XXX&Password=YYY
Just replace the XXX and YYY with your real user/pass - make sure it works.
Then use:
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("Username","Your username"));
nameValuePairs.add(new BasicNameValuePair("Password","Your password"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
Did you set the User-Agent filed?Like that:
get.setHeader("Content-Type", "text/html");
get.setHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 2.0.50215;)");
get.setHeader("Accept-Charset", Chareset+";q=0.7,*;q=0.7");//"utf-8;q=0.7,*;q=0.7");
And maybe you should get the login page firstly,to use the cookie.
If all this don't work,you shloud use some tools like firebug+firefox to track the network process,and emu them step by step.That should work.
Websites check some fields,I think this is definitely the reason.