HTTPS connection with basic auth result into Unauthorized

前端 未结 4 1993
名媛妹妹
名媛妹妹 2020-12-06 07:05

I am trying to access Basecamp API from my Android/Java source code following way ....

import org.apache.http.HttpResponse;
import org.apache.http.StatusLine         


        
4条回答
  •  春和景丽
    2020-12-06 07:34

    Finally I got it How to glue the code shown in above answer ...

    public static void performPost(String getUri, String xml) {
    
        String serverName = "*******";
        String username = "*******";
        String password = "********";
        String strResponse = null;
    
        try {
            HttpHost targetHost = new HttpHost(serverName, 443, "https");
    
            DefaultHttpClient httpclient = new DefaultHttpClient();
            try {
                // Store the user login
                httpclient.getCredentialsProvider().setCredentials(
                        new AuthScope(targetHost.getHostName(), targetHost.getPort()),
                        new UsernamePasswordCredentials(username, password));
    
                // Create AuthCache instance
                AuthCache authCache = new BasicAuthCache();
                // Generate BASIC scheme object and add it to the local
                // auth cache
                BasicScheme basicAuth = new BasicScheme();
                authCache.put(targetHost, basicAuth);
    
                // Add AuthCache to the execution context
                BasicHttpContext localcontext = new BasicHttpContext();
                localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
    
                // Create request
                // You can also use the full URI http://www.google.com/
                HttpPost httppost = new HttpPost(getUri);
                StringEntity se = new StringEntity(xml,HTTP.UTF_8);
                se.setContentType("text/xml");
                httppost.setEntity(se); 
                // Execute request
                HttpResponse response = httpclient.execute(targetHost, httppost, localcontext);
    
                HttpEntity entity = response.getEntity();
                strResponse = EntityUtils.toString(entity);
    
                StatusLine statusLine = response.getStatusLine();
                Log.i(TAG +": Post","statusLine : "+ statusLine.toString()); 
                Log.i(TAG +": Post","________**_________________________\n"+strResponse);
                Log.i(TAG +": Post","\n________**_________________________\n");
    
            } finally {
                httpclient.getConnectionManager().shutdown();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    
    }
    

    One very important thing How your library should be arranged and which libraries you will required ...

    enter image description here

    From Here you will find this libraries.

    To add them in eclipse (Below Android sdk < 16)...

    Project properties -> java build path -> Libraries -> Add external JARs
    

    To arrange them in order in eclipse ...

    Project properties -> java build path -> order and export
    

    For above Android sdk >= 16 you will have to put these libraries into "libs" folder.

提交回复
热议问题