I know there are a few posts on this topic, but I just can figure out what I’m doing wrong.
I have to send by post some parameter to a php server that requires a login.
Here is the code:
DefaultHttpClient httpclient = new DefaultHttpClient();
String postUrl = "http://dev.demo.fr/Contacts/areaGet.php";
HttpHost targetHost = new HttpHost("dev.demo.fr", 80, "http");
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials("*****", "*****"));
HttpPost httppost = new HttpPost(postUrl);
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("language_id", "1"));
nameValuePairs.add(new BasicNameValuePair("country_id", "1"));
nameValuePairs.add(new BasicNameValuePair("postal_code", "42830"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
response = httpclient.execute(targetHost, httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
System.out.println(response);
IMPORTANT: The username and password are uncoded in here:
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials("*****", "*****"));
The problem is that when I do this: System.out.println(response); it prints out null and I just don’t know why!!!!
Thank you for your answers!!!
if you get response “null” from server it means your request is not successfully posted on server.