无法使用身份验证从morningstar.com下载csv文件

时间:2013-03-29 14:06:27

标签: java apache authentication

我在morningstar有一个高级帐户,我试图从高级内容区域下载一些csv文件。出于某种原因,我无法获得这些优质内容。例如,使用高级帐户,我可以获得10年的财务报表数据,但我已经尝试了来自apache httpcomponents-client的所有示例身份验证Java代码。所有这些都只能让我得到不需要身份验证的内容。如何判断morningtar使用的身份验证协议并成功进行身份验证?我尝试了org.apache.http.examples.client中的示例代码,包括clientAuthentication.java,clientKerberosAuthentication.java,clientInteractiveAuthentication.java。如果我在Chrome中登录晨星帐户并粘贴此URL,我可以获得10年的数据csv,但如果我通过java访问,我只能获得5年的数据。以下是我尝试过的示例代码之一。我没有得到例外或错误,但我只有5年而不是10年。

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
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;


public class ClientAuthentication {

    public static void main(String[] args) throws Exception {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        try {
            httpclient.getCredentialsProvider().setCredentials(
                    new AuthScope("morningstar.com", 443),
                    new UsernamePasswordCredentials("xxx@gmail.com", "xxxx")); //anonymized this before posting to stackoverflow

            HttpGet httpget = new HttpGet("http://financials.morningstar.com/ajax/ReportProcess4CSV.html?t=aapl&region=usa&culture=en_US&reportType=is&period=12&dataType=A&order=asc&columnYear=10&rounding=3&view=raw&productCode=USA&r=199209&denominatorView=raw&number=3");

            System.out.println("executing request" + httpget.getRequestLine());
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            BufferedReader in;
            in = new BufferedReader(new InputStreamReader(entity.getContent()));

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            if (entity != null) {
                System.out.println("Response content length: " + entity.getContentLength());
                int linenum = 0;
                while (true){
                    String line = in.readLine();
                    if (line == null) break;
                    linenum++;  
                    if (linenum>1)
                    System.out.println(line);

                }
            }
            EntityUtils.consume(entity);
        } finally {
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
            httpclient.getConnectionManager().shutdown();
        }
    }
}

0 个答案:

没有答案