I'm trying to do a post to a url by sending a username and a password using the post method.But the answer is unclear.
Here is how I do it:
HttpClient httpclient =new DefaultHttpClient();
HttpPost httppost=new HttpPost("http://test.res-novae.fr/xperia_orange/scripts/android_ckeck_user.php");
try{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
/*Checking response*/
if(response!=null)
{
InputStream in = response.getEntity().getContent();
System.out.println("Resultat de la server:"+in);
}
}
catch(ClientProtocolException e)
{
}
catch (IOException e) {
// TODO Auto-generated catch block
}
and this is the answer from server:
Resultat de la server:org.apache.http.conn.EofSensorInputStream#44e067e0
Does anyone know what I'm doing wrong?
You are not reading the stream, but are rather looking at a String representation of the underlying InputStream object.
You will want to read the contents of the InputStream. Take a look at this question for examples of how to do this.
Related
I am new to android and I have a question about name value pairs that I am a little confused on. For example I am trying to post to the following example and get the response code using the endpoint:
/account/create/bank-id?ssn=SSN&name=NAME&email=EMAIL. I would edit the need to edit the following : bank-id , SSN , NAME, and EMAIL. at the moment. I am thinking along the lines of(so far no luck , no repsponse code are printing so I dont know what I am doing wrong at the moment, ty for any replies):
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://xx.xxx.xxx/account/create/");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("bank-id", "12345"));
nameValuePairs.add(new BasicNameValuePair("ssn", "451"));
nameValuePairs.add(new BasicNameValuePair("name", "kitty"));
nameValuePairs.add(new BasicNameValuePair("name", "kitty"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
Log.d("Http Post Response:", response.toString());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
How do I send the data to this PHP file below? I can establish the connection and send data, but can't receive the response. I need to send 3 parameters, the "op", Username and password.
switch ($_POST["op"]) {
// User Authentication.
case 1:
$UName = $_POST["UName"];
$UPass = $_POST["UPass"];
$UPass = md5($UPass);
//....Some code
// New user registration process.
case 2:
$UName = $_POST["UName"];
$UPass = $_POST["UPass"];
$UEmail = $_POST["UEmail"];
$UNick = $_POST["UNick"];
$UPass = md5($UPass);
//....Some code
}
My code so far:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://SomeUrl/login.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("1", "dan"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
String responseBody = EntityUtils.toString(response.getEntity());
Log.d(TAG, ""+responseBody);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
What am I doing wrong?
Looking at your PHP code, at a bare minimum you need to specify an op parameter. Then depending on which op you're trying to carry out, make sure you specify the other needed parameters as well. For user authentication (op = 1):
nameValuePairs.add(new BasicNameValuePair("op", "1"));
nameValuePairs.add(new BasicNameValuePair("UName", "dan"));
nameValuePairs.add(new BasicNameValuePair("Pass", "somepass"));
On a side note, you should secure the PHP service with SSL, if you are using it to process sensitive user information.
Android HTTP POST should look like this:
HttpParams httpParams=new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
HttpConnectionParams.setSoTimeout(httpParams, 10000);
// Data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("UName", "dan"));
nameValuePairs.add(new BasicNameValuePair("UPass", "password"));
nameValuePairs.add(new BasicNameValuePair("op", "1"));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://SomeUrl/login.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
}
In your PHP code I would strongly recommend to use mysql_real_escape_string($_POST['UName']) otherwise it is easy to attack via SQL injection.
I would also recommend to use either SSL connection (HTTPS) or to send the password only hashed (MD5)
I need to be able to post the form at http:///Default.aspx . I have tried I think all the different possible combinations of parameters that I thought I need to pass along, but I have not had any success.
I want to be able to post through an android code, but above that, I feel it is a problem with the request I am making for the post. Here is my code that attempts to post the form :
private void Post() {
// TODO Auto-generated method stub
// Create a new HttpClient and Post Header
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://customer.chuckwilson.com/Default.aspx");
httppost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0");
httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");
httppost.setHeader("Accept-Charset", "utf-8");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("__VIEWSTATE", "<state value>"));
nameValuePairs.add(new BasicNameValuePair("__EVENTVALIDATION", "<Event validation value>"));
nameValuePairs.add(new BasicNameValuePair("txtEmailAddress", "email#android.com"));
nameValuePairs.add(new BasicNameValuePair("txtStreetAddress", "streetandroid"));
nameValuePairs.add(new BasicNameValuePair("txtZipCode", "5454"));
nameValuePairs.add(new BasicNameValuePair("txtCity", "cityandroid"));
nameValuePairs.add(new BasicNameValuePair("CallBack", "rdCallBackYes"));
nameValuePairs.add(new BasicNameValuePair("txtLastName", "lastandroid"));
nameValuePairs.add(new BasicNameValuePair("phone", "(111) 111-1111"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
Log.d(getPackageName(), "executed http post req");
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
Log.i("RESPONSE",EntityUtils.toString(resEntity));
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
Log.e(getPackageName(), "error1 in req");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e(getPackageName(), "error2 in req");
e.printStackTrace();
}
}
Everytime I run the code, the response I get is the html for that form. That indicates that my parameters are wrong, but I really don't see anything wrong with it. I hope someone can point the mistake/s out. Any help is much appreciated.
The form in the page you link to is defined with this HTML tag:
<form name="form1" method="post" action="Default.aspx" id="form1" enctype="multipart/form-data">
The "enctype" means it is not using the URL-Encoded form submission that you are producing, but the MIME multipart protocol that's normally used for file uploads.
See this blog article, which shows how to use this type of form: http://blog.tacticalnuclearstrike.com/2010/01/using-multipartentity-in-android-applications/
I have android app . i should pass the data from android to web server with data security.Can any one suggest me to What are all the data security's can Provide.
You can send the data using HttpPost methos .
This is a simple example for it .
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
Use Https , you should buy a SSL for your server first , and then Use https to send data.
I am trying to retrieve a response from my server which ultimately redirects to a URL after post data is submitted. I figured I could retrieve the url response that the server redirects with , with the code below but every time I do i get a fatal error out of memory. I was wondering what may cause this error if any one had any ideas of why I am getting this error or if this is an incorrect way? I have tested I am sending the data correctly to the server but keep getting this error. here is my code.
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example.com/");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("token", token));
nameValuePairs.add(new BasicNameValuePair("key", "test"));
nameValuePairs.add(new BasicNameValuePair("mac", "test"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
ResponseHandler<String> responseHandler=new BasicResponseHandler();
String responseBody = httpclient.execute(httppost, responseHandler);
Log.d("URL", responseBody);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}