1

下面的代码是我到目前为止所尝试的:

try{
            DefaultHttpClient httpclient = new DefaultHttpClient();

            HttpGet httpget = new HttpGet("http://somewebsite.com");

            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();

            System.out.println("Login form get: " + response.getStatusLine());
            if (entity != null) {
                entity.consumeContent();            
            }
            System.out.println("Initial set of cookies:");
            List<Cookie> cookies = httpclient.getCookieStore().getCookies();
            if (cookies.isEmpty()) {
                System.out.println("None");
            } else {
                for (int i = 0; i < cookies.size(); i++) {
                    System.out.println("- " + cookies.get(i).toString());
                }
            }

            HttpPost httpost = new HttpPost("http://www.somewebsite.com/MemberSingIn");

            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("email", "USERNAME"));
            nvps.add(new BasicNameValuePair("password", "PASSWORD"));
            nvps.add(new BasicNameValuePair("rememberMe", "true"));

            httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

            response = httpclient.execute(httpost);
            entity = response.getEntity();

            System.out.println("Login form get: " + response.getStatusLine());
            if (entity != null) {
                entity.consumeContent();
            }

            System.out.println("Post logon cookies:");
            cookies = httpclient.getCookieStore().getCookies();
            String mySessionId = null;
            if (cookies.isEmpty()) {
                System.out.println("None");
            } else {
                for (int i = 0; i < cookies.size(); i++) {
                    if(cookies.get(i).toString().contains("SessionId=")){
                        int index1 = cookies.get(i).toString().indexOf("SessionId");
                        String temp = cookies.get(i).toString().substring(index1);
                        int index2 = temp.toString().indexOf("]");
                        temp = cookies.get(i).toString().substring(index1+10,index1+index2);
                        mySessionId = temp;
                    }
                    System.out.println("- " + cookies.get(i).toString());
                }
            }      

            CookieStore cookieStore = new BasicCookieStore(); 
            BasicClientCookie cookie = new BasicClientCookie("Cookie",mySessionId);

            cookieStore.addCookie(cookie); 
            httpclient.setCookieStore(cookieStore);

            httpget = new HttpGet("http://somewebsite.URLTOGETFILE/eTable.txt");
            response = httpclient.execute(httpget);
            entity = response.getEntity();
            System.out.println("HEREEEEEEEEEEEEEEEEEEE");
            System.out.println(response.toString());

            System.out.println("File get: " + response.getStatusLine());

            InputStream in = entity.getContent();


              File path = new File("/home/mrsevensevenseven/");
              path.mkdirs();
              File file = new File(path, "myfile.txt");
              FileOutputStream fos = new FileOutputStream(file);

              byte[] buffer = new byte[1024];
              int len1 = 0;
              while ((len1 = in.read(buffer)) > 0) {
                      fos.write(buffer, 0, len1);
              }

              fos.close();

            httpclient.getConnectionManager().shutdown();
        }catch(Exception e){
            e.printStackTrace();
        }

它成功,让我登录,但我仍然无法下载文件......在这方面的任何帮助将不胜感激......我在堆栈溢出和谷歌搜索方面尝试了很多,但无法获得帮助......

这就是回应...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>
    IBD eTables - Investors.com
</title><meta property="og:title" content="IBD eTables" /><meta property="og:type" content="article" /><meta property="og:url" content="http://research.investors.com/etables/default.aspx" /><meta property="og:image" content="http://www1.ibdcd.com/images/IBDicon_309171.png" /><meta property="og:site_name" content="Investor's Business Daily" /><meta property="fb:app_id" content="154916351273272" />

    <script type="text/javascript">
        var divHeight;
    </script>
<link href="../App_Themes/eTables/eTablesStyles.css" type="text/css" rel="stylesheet" />
    <script type="text/javascript" src="/Scripts/global.js?v=4"></script>

    <script type="text/javascript" src="/Scripts/Silverlight.js?v=4"></script>

    <script type="text/javascript" src="/Scripts/LaunchMSTool.js?v=4"></script>
</head>

<body>
<form name="form1" method="post" action="/etables/default.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTExMzcwMDM5OTcPFhIeDEVuYWJsZUV4cG9ydGceC0N1cnJlbnRWaWV3AgEeDUN1cnJlbnRWaWV3SWQoKVtTeXN0ZW0uRGVjaW1hbCwgbXNjb3JsaWIsIFZlcnNpb249Mi4wLjAuMCwgQ3VsdH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YXJjaGQCIQ8WAh8KaGQCIg8PFgIfCmdkFgICAQ8PFgIfDAXUASogSUJEIDUwIHN0b2NrcywgcmFua2luZ3MsIGNvbXBhbnkgc3VtbWFyaWVzLCBhbmQgY2hhcnQgYW5hbHlzaXMgd2VyZSBsYXN0IHVwZGF0ZWQgMy8yMi8yMDEzLCBhbmQgYXJlIHVwZGF0ZWQgd2Vla2x5LiBPdGhlciBJQkQgNTAgZGF0YSBmZWF0dXJlcywgaW5jbHVkaW5nIHByaWNlLCB2b2x1bWUgYW5kIFNtYXJ0U2VsZWN0IFJhdGluZ3MgYXJlIHVwZGF0ZWQgZGFpbHkuZGQYBQUPSGVhZGVyTXVsdGlWaWV3Dw9kAgJkBQ9SZXN1bHRNdWx0aVZpZXcPD2QCAWQFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYBBRZJbmRleEJhckNoYXJ0JHdjdkNoYXJ0BQ1NdWx0aVZpZXdTb3J0Dw9kZmQFCk11bHRpVmlldzEPD2RmZB2Z4W+QIKnJSe4+7+2PiFBPu1ck" />
</div>


<script src="/ScriptResource.axd?d=RNfHbK6XxGlvk7UnQPQl5VWdSmPtL605im1i9ZMMuXgrvwYOrYkRhhjS5Tkc95LZKqMA01lmkm3Rvv6OCYTXQO0Wvej6DzniFUlixhkXWzmpvwOMQ0v-ompHlYoQzp1H0LzWltEL_D_EvsXg_ayZnY1oHnA1&amp;t=ffffffff8dc250fc" type="text/javascript"></script>
<script src="../Services/SiteAjaxService.asmx/js" type="text/javascript"></script>






<a id="hlRoadBlock" class="ex5trigger" rel="/Register/Roadblock.aspx?id=ETB&closepop=1" href="javascript:void(0);"></a>

    <a id="hlSignInRoadBlock" class="ex5trigger" rel="/Register/WeeklyEmails.aspx?id=etb" href="javascript:void(0);"></a>            

    <script type="text/javascript">
        var isEntitled = ToBoolean("false");
        var isWeekly = ToBoolean("false");
    </script>
    <script type="text/javascript" src="/Scripts/ExternalScripts/eTables.js"></script>


<div id="etbtop"></div>
<div id="etbmiddle">
    <div id="etbDummyImage" style="display:none;">
        <img id="staticeTableImage" src="http://www.investors.com/image/bgeTablesSm.png.cms" style="border-width:0px;" />
    </div>
    <div id="etbcontent" style="display:none;">

    </div>
</div>

<div id="etbbottom"></div> 
<div class="jqmWindow" id="ex5"></div>





<script type="text/javascript">
//<![CDATA[
var iLoginFlag = 0;var sAccess = "PVC";var sDGOProductsSite = "premium.investors.com";//]]>
</script>
</form>

    <!-- BEGIN: Site Tagging -->
    <script type="text/javascript" language="javaScript">if (typeof(s) != 'undefined'){s.server="IBDWEB21";s.events="";s.channel="Stock Research";s.pageName="";s.pageType="";s.hier1="Stock Research,eTables";s.products="";s.state="";s.zip="";s.purchaseID="";s.prop1="";s.prop2="";s.prop3="eTables";s.prop4="";s.prop5="";s.prop6="";s.prop7="";s.prop8="";s.prop9="";s.prop10="Tools";s.prop11="";s.prop12="Visitor";s.prop13="";s.prop14="";s.prop15="";s.prop16="eTables";s.prop17="";s.prop18="";s.prop19="";s.prop20="";s.prop21="";s.prop22="";s.prop23="";s.prop24="";s.prop32="";s.prop33="";s.prop34="";s.prop35="";s.prop36="";s.prop37="";s.prop38="";s.prop39="";s.prop40="";s.prop41="";s.prop42="";s.prop43="";s.prop44="";s.prop45="";s.prop46="";s.prop47="";s.prop48="";s.prop49="";s.eVar1="";s.eVar2="";s.eVar3="";s.eVar4="";s.eVar5="";s.eVar6="";s.eVar7="";s.eVar8="";s.eVar9="";s.eVar10="";s.eVar11="Visitor";s.eVar12="";s.eVar13="";s.eVar14="";s.eVar15="";s.eVar16="eTables";s.eVar17="";s.eVar18="";s.eVar19="";s.eVar20="";s.eVar21="";s.eVar22="";s.eVar23="";s.eVar24="";s.eVar25="";s.eVar26="";s.eVar27="";s.eVar28="";s.eVar29="";s.eVar30="";s.eVar31="";s.eVar32="";s.eVar33="";s.eVar34="";}</script>
    <script type="text/javascript" language="javascript">
    // custom variables
    if (typeof(s) != 'undefined')
    {
        s.pageName = "eTables - IBD 50 Index - Standard Stock Data";
    }
    </script>
    <script type="text/javascript" language="javaScript">if (typeof(s) != 'undefined'){try{var s_code=s.t();if(s_code){document.write(s_code);}}catch(e){}}</script>
    <!-- END: Site Tagging -->


    <script type="text/javascript" src="/Scripts/etables.js?v=4"></script>

    <script type="text/javascript" src="/Scripts/ResearchAddToStockList.js?v=4"></script>


    <a id="ServerTagLink" href="http://IBDWEB21">
        <img src="http://www1.ibdcd.com/Images/eTables/px.gif" width="20" height="20" alt="" />
    </a>



    <script type="text/javascript">
        document.write(unescape("%3Cscript src='" + (document.location.protocol == "https:" ? "https://sb" : "http://b") + ".scorecardresearch.com/beacon.js' %3E%3C/script%3E"));
    </script>
    <script type="text/javascript">
        COMSCORE.beacon({c1:2,c2:7592510,c3:"",c4:"",c5:"",c6:"",c15:""});
    </script>
    <noscript>
        <img src="http://b.scorecardresearch.com/p?c1=2&c2=7592510&c3=&c4=&c5=&c6=&c15=&cj=1" alt="" />
    </noscript>

</body>
</html>

问候...

777先生

4

1 回答 1

1

好的,所以经过更多研究并在@jdb 的帮助下,我能够解决这个问题,现在我可以在成功登录后从服务器下载文件而没有任何问题......顺便说一句,感谢@jdb为我指明正确的方向,让我思考到底出了什么问题:)

在这里粘贴代码,如果其他人想使用:

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.protocol.HTTP;


public class DownloadAuthenticatedFile{

    public static void main(String... args){

        try{
            DefaultHttpClient httpclient = new DefaultHttpClient();

            HttpGet httpget = new HttpGet("http://website.com");

            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();

            System.out.println("Login form get: " + response.getStatusLine());
            if (entity != null) {
                entity.consumeContent();            
            }
            System.out.println("Initial set of cookies:");
            List<Cookie> cookies = httpclient.getCookieStore().getCookies();
            if (cookies.isEmpty()) {
                System.out.println("None");
            } else {
                for (int i = 0; i < cookies.size(); i++) {
                    System.out.println("- " + cookies.get(i).toString());
                }
            }

            HttpPost httpost = new HttpPost("http://www.somewebsite.com/MemberSingIn");

            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("email", "USERNAME"));
            nvps.add(new BasicNameValuePair("password", "PASSWORD"));
            nvps.add(new BasicNameValuePair("remember", "true"));

            httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

            response = httpclient.execute(httpost);
            entity = response.getEntity();

            System.out.println("Login form get: " + response.getStatusLine());
            if (entity != null) {
                entity.consumeContent();
            }

            System.out.println("Post logon cookies:");
            cookies = httpclient.getCookieStore().getCookies();
            String mySessionId = null;
            if (cookies.isEmpty()) {
                System.out.println("None");
            } else {
                for (int i = 0; i < cookies.size(); i++) {
                    if(cookies.get(i).toString().contains("SessionId=")){
                        int index1 = cookies.get(i).toString().indexOf("SessionId");
                        String temp = cookies.get(i).toString().substring(index1);
                        int index2 = temp.toString().indexOf("]");
                        temp = cookies.get(i).toString().substring(index1+10,index1+index2);
                        mySessionId = temp;
                    }
                    System.out.println("- " + cookies.get(i).toString());
                }
            } 

            httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
            httpget = new HttpGet("http://PATHOFWEBSITETOFILE");
            response = httpclient.execute(httpget);
            entity = response.getEntity();
            Header[] headers = response.getAllHeaders();
            for(int i=0;i<headers.length;i++){
                System.out.println("Header: "+headers[i].toString());
            }
            System.out.println(response.toString());

            System.out.println("File get: " + response.getStatusLine());

            InputStream in = entity.getContent();
              File path = new File("/home/mrsevensevenseven");
              path.mkdirs();
              File file = new File(path, "myfile.xls");
              FileOutputStream fos = new FileOutputStream(file);

              byte[] buffer = new byte[1024];
              int len1 = 0;
              while ((len1 = in.read(buffer)) != -1) {
                      fos.write(buffer, 0, len1);
              }

              fos.close();

            httpclient.getConnectionManager().shutdown();
        }catch(Exception e){
            e.printStackTrace();
        }

    }

}
于 2013-03-25T21:48:47.353 回答