As the topic indicates I'm trying to perform a http post request to a page 
secured with basic http authentication.

It works from the command line with curl as following:
curl http://snot%40snot.dk:supersnot_at_teletracker.herokuapp.com/devices 
-d "device[name]=snot" -d "device[device_id]=1234"

But with the java code below I get redirected to a login form. Any ideas 
what I'm doing wrong?
Note that the username and password I have supplied is working so, if 
anyone cares, please try it out. You just have to replace _at_ with @


    private void sendDeviceIdToServer(String deviceId) {
    DefaultHttpClient client = new DefaultHttpClient();
    client.getCredentialsProvider().setCredentials(
    new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
    new UsernamePasswordCredentials("snot_at_snot.dk", "supersnot"));
    //new UsernamePasswordCredentials("snot%40snot%2Edk", "supersnot"));
    HttpPost post = new 
HttpPost("http://teletracker.herokuapp.com/devices";);
    try {
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    nameValuePairs.add(new BasicNameValuePair("device[name]", "from 
android"));
    nameValuePairs.add(new BasicNameValuePair("device[device_id]", 
deviceId));
    
    post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    ResponseHandler<String> handler = new BasicResponseHandler();
    String response = client.execute(post, handler);
    Log.d("HttpResponse", response);
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to