基本身份验证结果为未授权的HTTPS连接身份验证、基本、结果、HTTPS

由网友(◆來罘及握手)分享简介:我想从我的Andr​​oid / Java源代码code以下方式访问大本营API .. .. I am trying to access Basecamp API from my Android/Java source code following way ....import org.apache.http.H...

我想从我的Andr​​oid / Java源代码code以下方式访问大本营API .. ..

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;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class BCActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        DefaultHttpClient httpClient = new DefaultHttpClient();

        //final String url = "https://encrypted.google.com/webhp?hl=en"; //This url works
        final String url = "https://username:password@projectsource.basecamphq.com/people.xml"; //This don't
        HttpGet http = new HttpGet(url);
        http.addHeader("Accept", "application/xml");
        http.addHeader("Content-Type", "application/xml"); 

        try {

            //  HttpResponse response = httpClient.execute(httpPost);
            HttpResponse response = httpClient.execute(http);

            StatusLine statusLine = response.getStatusLine();
            System.out.println("statusLine : "+ statusLine.toString()); 

            ResponseHandler <String> res = new BasicResponseHandler();  

            String strResponse = httpClient.execute(http, res);
            System.out.println("________**_________________________n"+strResponse);
            System.out.println("n________**_________________________n");

        } catch (Exception e) {
            e.printStackTrace(); 
        }

        WebView myWebView = (WebView) this.findViewById(R.id.webView);
        myWebView.loadUrl(url);//Here it works and displays XML response

    }
}

这个URL显示在的WebView 的响应,但显示未授权的例外,当我试图通过的HttpClient 来访问,如图上方。

This URL displays the response in WebView, but shows Unauthorized exception when I try to access through HttpClient as shown above.

这是正确的方式通过的Andr​​oid / Java访问的的Basecamp API ? 要么 请给我一个正确的方式来做到这一点。

Is this is right way to access Basecamp API through Android/Java? or Please provide me a right way to do so.

推荐答案

在的HttpClient 的不能把从URI登录creditals。 你必须给他们指定的方法。

The HttpClient can't take the login creditals from the URI. You have to give them with specified methods.

如果您使用的的HttpClient 的4.x中对此看看: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html

If you use HttpClient 4.x have a look on this: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html

但请注意,如果你不希望使用新版本上的的HttpClient 的(Android使用3.x版本),你应该看看这里: http://hc.apache.org/httpclient-3.x/authentication.html

But notice if you don't want to use the new version on the HttpClient (Android uses version 3.x), you should look here: http://hc.apache.org/httpclient-3.x/authentication.html

这是理论,现在我们使用它们: 基本上,我们使用的 HTTP 的,但如果你想使用的 HTTPS 的,你必须编辑以下分配新HttpHost(www.google.com 80,HTTP)新HttpHost(www.google.com,443,https开头)

That was the theory, now we use them: Basically we use HTTP, but if you want to use HTTPS, you have to edit the following assignment new HttpHost("www.google.com", 80, "http") into new HttpHost("www.google.com", 443, "https").

此外,你必须编辑主机( www.google.com 的),为您的关注。 说明:只有完全限定域名(FQDN)是必要的并不是完整的URI

Furthermore you have to edit the host (www.google.com) for your concerns. Notice: Only the full qualified domain name (FQDN) is needed not the full URI.

的HttpClient 的3.X:

HttpClient 3.x:

package com.test;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.os.Bundle;

public class Test2aActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {
            HttpHost targetHost = new HttpHost("www.google.com", 80, "http");

            DefaultHttpClient httpclient = new DefaultHttpClient();
            try {
                // Store the user login
                httpclient.getCredentialsProvider().setCredentials(
                        new AuthScope(targetHost.getHostName(), targetHost.getPort()),
                        new UsernamePasswordCredentials("user", "password"));

                // Create request
                // You can also use the full URI http://www.google.com/
                HttpGet httpget = new HttpGet("/");
                // Execute request
                HttpResponse response = httpclient.execute(targetHost, httpget);

                HttpEntity entity = response.getEntity();
                System.out.println(EntityUtils.toString(entity));
            } finally {
                httpclient.getConnectionManager().shutdown();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

的HttpClient 的4.x的:

HttpClient 4.x:

注意:您需要从Apache的新的的HttpClient 的,此外你必须重新排列顺序,该JAR文件是Android库之前

Attention: You will need the new HttpClient from Apache and additionally you must rearrange the order, that the jar-file is before the Android library.

package com.test;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.os.Bundle;

public class TestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {
            HttpHost targetHost = new HttpHost("www.google.com", 80, "http");

            DefaultHttpClient httpclient = new DefaultHttpClient();
            try {
                // Store the user login
                httpclient.getCredentialsProvider().setCredentials(
                        new AuthScope(targetHost.getHostName(), targetHost.getPort()),
                        new UsernamePasswordCredentials("user", "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/
                HttpGet httpget = new HttpGet("/");
                // Execute request
                HttpResponse response = httpclient.execute(targetHost, httpget, localcontext);

                HttpEntity entity = response.getEntity();
                System.out.println(EntityUtils.toString(entity));
            } finally {
                httpclient.getConnectionManager().shutdown();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
阅读全文

相关推荐

最新文章