Android Java Http Post to PHP server is always NULL? - java

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.

Related

Authenticate with CouchDB and insert new database via HTTP PUT in Android app

I am implementing an Android app that should upload data to CouchDB. Since I have restricted the admin access to one account, I have to authenticate before inserting a new database. And this is what I am currently struggling with: Authenticate and insert a new database. Operating via Terminal and using curl, everything is working out fine the following way:
> curl -X PUT http://admin_name:admin_password#url:port/database_to_be_inserted
First approach
My first approach was to simply do the same via HTTP PUT in my code like that:
private boolean putJSON(String json, String url) {
// url = http://admin_name:admin_password#url:port/database_to_be_inserted
HttpClient client = new DefaultHttpClient();
HttpPut put = new HttpPut(url);
try {
StringEntity stringEntity = new StringEntity(json,"utf-8");
put.setEntity(stringEntity);
put.setHeader("Content-type", "application/json; charset=utf-8");
put.setHeader("Accept", "application/json");
HttpResponse response = client.execute(put);
// ... buffered input reading on response...
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
However, doing so I retrieve the following error and JSON array:
Authentication error: Unable to respond to any of these challenges: {}
{"error":"unauthorized","reason":"You are not a server admin."}
The point is, that using the same method for inserting a new user works out perfectly. So, if I am using the above method with a correctly formatted user JSON-Dictionary and the following url, the user is inserted correctly.
http://admin_name:admin_password#url:port/_users/org.couchdb.user:user_name
This should prove, that I am using the right admin data at least, shouldn't it?
Second approach
So, by now, I am trying to authenticate using the "Authorization" option in my HTTP PUT's header:
private boolean putDatabase(String userName, String password, String url) {
// url = "http://url:port/database_to_be_inserted"
HttpClient client = new DefaultHttpClient();
HttpPut put = new HttpPut(url);
String authenticationData = userName+":"+password;
String encoding = Base64.encodeToString(authenticationData.getBytes(Charset.forName("utf-8")), Base64.DEFAULT);
put.setHeader("Authorization", "Basic " + encoding);
try {
put.setHeader("Content-type", "application/json; charset=utf-8");
put.setHeader("Accept", "application/json");
HttpResponse response = client.execute(put);
// ... buffered input reading on response...
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
Still no success in inserting the database. The response I am parsing says:
Host not found
I have double checked the admin name, password, and url and everything seems correct. Does anyone of you see why this might not work out?
Ok, the answer is simple: The above code (at least the one of my second approach) is working fine. My mistake was to not explicitly specify the port via which the CouchDB should be accessed. This is, how I accidentally called the method:
putDatabase("adminName", "adminPassword", "http://url/database_to_be_inserted");
However, this is how I should have called it:
putDatabase("adminName", "adminPassword", "http://url:port/database_to_be_inserted");
Who is using iriscouch like me and does not know which port to specify here, can look it up in the config file. Using Futon this can be found in the entry "httpd > port" here:
> http://your_url_spec.iriscouch.com/_utils/config.html
More general and without Futon this can be found (and if you wish so edited) via command-line in the local.ini of your own CouchDB installation:
~$ cat etc/couchdb/local.ini

Log into php-based site and scraping data - problems

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();`

how to create issue using bitbucket API and singpost in java

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.

HTTP Get method based on parameters in java

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.

How to Authenticate Against Website Using Apache HttpClient 4.2.1 or >?

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.

Categories