[英]Java downloading text file results in html code instead of text
当我使用apache commons-io下载此文本文件asdf.txt时 :
URL fileURL = new URL(urlMatcher.group(1)); FileUtils.copyURLToFile(fileURL, new File(fileName));
它导致网页的html代码而不是
我是asdf文本文件!
它应该返回。
将URL打印为字符串可得到以下结果:
请帮我找出登录所需的POST请求的Java代码
主机= alm.automic.com
User-Agent = Mozilla / 5.0(Windows NT 6.1; WOW64; rv:39.0)Gecko / 20100101 Firefox / 39.0
Accept = text / html,application / xhtml + xml,application / xml; q = 0.9, / ; q = 0.8
Accept-Language = zh-CN,en; q = 0.5
Accept-Encoding = gzip,放气
引荐来源网址 = https://alm.automic.com/jama/login.req
Cookie = JSESSIONID = CA14239217D0E54E913D17083F71724F; DWRSESSIONID = 8nCO $ BHWFA2282XF4fVYnmwl7Wk; jamaContourServerTime = 1436778397165; jamaContourSessionExpiry = 1436778397165
连接=保持活动
Content-Type =应用程序/ x-www-form-urlencoded
内容长度= 53
POSTDATA = j_username = user&j_password = pass&submit =登录
请帮助,在此先感谢!
找到了如何进行http连接(如果有人需要):使用以下类和类似Temper Data(Firefox插件)的内容来查找GET / POST请求标头/数据,并以类似以下方式将其添加到以下类中。
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.