0

我在 Morningstar 有一个高级帐户,我尝试从高级内容区域下载一些 csv 文件。出于某种原因,我无法获得那些优质内容。例如,使用高级帐户我可以获得 10 年的财务报表数据,但我已经尝试了来自 apache httpcomponents-client 的所有示例身份验证 java 代码。所有这些都只能让我获得不需要身份验证的内容。我怎样才能知道 Morningtar 正在使用什么身份验证协议并成功进行身份验证?我尝试了来自 org.apache.http.examples.client 的示例代码,包括 clientAuthentication.java、clientKerberosAuthentication.java、clientInteractiveAuthentication.java。如果我在 Chrome 中登录 Morningstar 帐户并粘贴此 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();
        }
    }
}
4

0 回答 0