尝试从受密码保护的网站下载数据

时间:2015-07-13 14:04:41

标签: java authentication session-cookies password-protection

我有一个包含我想要访问的数据的网址。数据是以文本形式存在的,并且受密码保护......但是这里的东西......它是由不同网站上的登录密码保护的。每次我尝试访问数据URL时,都会收到HTTP 500错误。有关此问题的任何建议吗?我不认为这是一个非常常见的问题,因为我在很多Stackoverflow和Google搜索中都没有遇到过这个问题。

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

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 个答案:

没有答案