从受其他URL(Java)密码保护的URL下载文本

时间:2015-07-10 14:40:41

标签: java apache http authentication password-protection

我有一个包含我想要访问的数据的网址。数据采用文本形式,并受密码保护......但这里的东西......它受到不同网站的密码保护。我已经花了来解决这个问题。当我使用Apache HttpClient时,我可以很好地登录登录URL,但我无法弄清楚如何访问数据URL。每次我尝试访问数据URL时,都会收到HTTP 500错误。有关此问题的任何建议吗?考虑到我在许多Stackoverflow和Google搜索中没有遇到过这个问题,我认为这是一个非常常见的问题。如果你能提供帮助,那么多谢你:)

以下是我尝试使用的其中一个程序无效的示例...(有些信息是私有的,所以我更改了用户名,密码和网址)

package Apache1;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
 import java.net.*;
 import java.io.*;

/**
 * A simple example that uses HttpClient to execute an HTTP request against
 * a target site that requires user authentication.
 */
public class Apache2 {
    public static void main(String[] args) throws Exception {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
                new AuthScope("localhost", 443),
                new UsernamePasswordCredentials("myUsername", "myPW"));
        CloseableHttpClient httpclient = HttpClients.custom()
                .setDefaultCredentialsProvider(credsProvider)
                .build();
        try {
            HttpGet httpget = new HttpGet("LOGIN URL");
            System.out.println("Executing request " + httpget.getRequestLine());
            CloseableHttpResponse response = httpclient.execute(httpget);
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                EntityUtils.consume(response.getEntity());

                        URL oracle = new URL("DATA URL");
                        URLConnection yc = oracle.openConnection();
                        BufferedReader in = new BufferedReader(new InputStreamReader(
                                                    yc.getInputStream()));
                        String inputLine;
                        while ((inputLine = in.readLine()) != null) 
                            System.out.println(inputLine);
                        in.close(); 



            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }
}

这是我最近处理cookie的代码。我一直收到HTTP 500错误。

package Apache1;
//import org.apache.http.*;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
 import java.net.*;
 import java.io.*;

/**
 * A simple example that uses HttpClient to execute an HTTP request against
 * a target site that requires user authentication.
 */
public class Apache2 {
    public static void main(String[] args) throws Exception {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
                new AuthScope("localhost", 443),
                new UsernamePasswordCredentials("myUSERNAME", "myPASSWORD"));
        CloseableHttpClient httpclient = HttpClients.custom()
                .setDefaultCredentialsProvider(credsProvider)
                .build();
        try {
            HttpGet httpget = new HttpGet("LOGIN_WEBSITE");
            System.out.println("Executing request " + httpget.getRequestLine());
            CloseableHttpResponse response = httpclient.execute(httpget);
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                EntityUtils.consume(response.getEntity());

                        URL oracle = new URL("DATA_WEBSITE");
                        URLConnection yc = oracle.openConnection();
                        BufferedReader in = new BufferedReader(new InputStreamReader(
                                                    yc.getInputStream()));
                        String inputLine;
                        while ((inputLine = in.readLine()) != null) 
                            System.out.println(inputLine);
                        in.close(); 



            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }
}

0 个答案:

没有答案