首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java下载文本文件将生成html代码而不是文本。

Java下载文本文件将生成html代码而不是文本。
EN

Stack Overflow用户
提问于 2015-07-10 13:52:45
回答 1查看 144关注 0票数 0

当我使用apache下载这个文本文件asdf.txt时:

URL fileURL = new URL(urlMatcher.group(1)); FileUtils.copyURLToFile(fileURL, new File(fileName));

它将生成网页的html代码,而不是

我就是那个文本文件!

它应该会回来。

将url打印为字符串的结果如下:

https://alm.automic.com/jama/attachment/542/asdf.txt

请帮我找出登录所需的POST请求的Java代码

Host=alm.automic.com 用户代理=Mozilla/5.0(WindowsNT6.1;WOW64;rv:39.0) 接受=text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8 接受-语言=en-US,en;q=0.5 接受-编码=gzip,放气 Referer=https://alm.automic.com/jama/login.req Cookie=JSESSIONID=CA14239217D0E54E913D17083F71724F;DWRSESSIONID=8nCO$BHWFA2282XF4fVYnmwl7Wk;jamaContourServerTime=1436778397165;jamaContourSessionExpiry=1436778397165 连接=保持活力 Content-Type=application/x-www-form-urlencoded 内容长度=53 POSTDATA=j_username=user&j_password=pass&submit=Login

请帮忙,谢谢提前!

EN

回答 1

Stack Overflow用户

发布于 2015-07-14 11:23:18

了解如何进行http连接,如果有人需要的话:使用下面的类和类似脾气数据(Firefox addon)之类的东西来查找GET/POST请求头/数据,并以如下方式将其添加到下面的类中。

代码语言:javascript
复制
ArrayList<String> cookies = new ArrayList<String>(1);
        HttpClient httpclient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(link);

        // Request parameters and other properties.
        List<NameValuePair> params = new ArrayList<NameValuePair>(2);
        httpGet.addHeader("Host", "alm.automic.com");
        httpGet.addHeader("User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0");
        httpGet.addHeader("Accept",
                "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        httpGet.addHeader("Accept-Language", "en-US,en;q=0.5");
        httpGet.addHeader("Accept-Encoding", "gzip, deflate");
        if (cookies.size() > 1)
            httpGet.addHeader("Cookie",
                    cookies.get(cookies.size() - 1).split(";")[0]);
        httpGet.addHeader("Connection", "keep-alive");

        // Execute and get the response.
        HttpResponse response = httpclient.execute(httpGet);
        Header[] responseHeaders = response.getAllHeaders();
        String location = "https://alm.automic.com/jama/login.req;";

        for (int i = 0; i < responseHeaders.length; i++)
        {
            if (responseHeaders[i].getName().equals("Set-Cookie"))
            {
                if (responseHeaders[i].getValue().split(";")[0].split("=")[0]
                        .equals("JSESSIONID"))
                    cookies.add(responseHeaders[i].getValue());
            }
            if (responseHeaders[i].getName().equals("Location"))
            {
                location = responseHeaders[i].getValue();
            }
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31342556

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档