[英]Response from HttpURLConnection in JAVA yields garbage value
场景:我需要点击一个HTTPS URL来获取.csv文件或其内容。 我可以使用Chrome浏览器中的Advanced REST客户端插件成功获取所需的内容。 但是在尝试使用Java代码执行相同操作时会得到一个垃圾值。
码:
public class AnotherBean {
private final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36";
public void printAnotherMessage() {
System.out.println("I am called by Quartz jobBean using CronTriggerFactoryBean");
AnotherBean anotherBean = new AnotherBean();
try {
System.out.println("Testing 1 - Send Http GET request");
anotherBean.sendGet();
// System.out.println("\nTesting 2 - Send Http POST request");
// anotherBean.sendPost();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// HTTP GET request
private void sendGet() throws Exception {
String url = "https://xxx.xxx.com/xxxx/DownloadReport/2015/10/29/xxx/TransactionDetailReport.csv";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
// add request header
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.8");
con.setRequestProperty("Content-Type", "text/plain; charset=utf-8");
con.setRequestProperty("Accept-Encoding", "gzip, deflate, sdch");
String urlParameters = "username=xxxx&password=xxxx";
con.setDoOutput(true);
con.setDoInput(true);
OutputStream wr = con.getOutputStream();
// wr.writeBytes(urlParameters);
wr.write(urlParameters.getBytes("UTF-8"));
wr.flush();
wr.close();
// con.connect();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
System.out.println("Post parameters : " + urlParameters);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// print result
System.out.println("Response: " + response.toString());
System.out
.println("Content Encoding: " + con.getContentEncoding() + "\nContent Length: " + con.getContentLength()
+ "\nContent Type: " + con.getContentType() + "\nContent: " + con.getContent());
}
// HTTP POST request
private void sendPost() throws Exception {
String url = "https://xxx.xxx.com/xxxx/DownloadReport/2015/10/29/xxx/TransactionDetailReport.csv";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
// add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.8");
con.setRequestProperty("Content-Type", "text/plain; charset=utf-8");
con.setRequestProperty("Accept-Encoding", "gzip, deflate, sdch");
String urlParameters = "username=xxx&password=xxxx";
// String urlParameters = "{" +
// "\"username\":\"xxxx\",\"password\":\"xxxx\"" +
// "}";
// Send post request
con.setDoOutput(true);
con.setDoInput(true);
// DataOutputStream wr = new DataOutputStream(con.getOutputStream());
OutputStream wr = con.getOutputStream();
// wr.writeBytes(urlParameters);
wr.write(urlParameters.getBytes("UTF-8"));
wr.flush();
wr.close();
// con.connect();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
// BufferedReader inputReader = new BufferedReader(new
// InputStreamReader((InputStream) con.getContent()));
// String theString = IOUtils.toString(con.getInputStream(),
// StandardCharsets.UTF_16BE);
// byte[] byteArray = IOUtils.toByteArray(con.getInputStream());
// String theString = IOUtils.toString(byteArray);
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
// System.out.println(" " + inputLine);
}
/*
* do { inputLine = inputReader.readLine(); response.append(inputLine +
* "\n"); } while (inputLine != null);
*/
// String finalResponse = new String(response, StandardCharsets.UTF_8);
// inputReader.close();
// print result
System.out.println("Response: " + response.toString());
// System.out.println("Response - theString: " + theString);
// System.out.println("Response String: " + byteArray);
System.out
.println("Content Encoding: " + con.getContentEncoding() + "\nContent Length: " + con.getContentLength()
+ "\nContent Type: " + con.getContentType() + "\nContent: " + con.getContent());
}
}
**Response:**
***Sending 'GET' request to URL :*** "https://xxx.xxx.com/xxxx/DownloadReport/2015/10/29/xxx/TransactionDetailReport.csv";
***Response Code :*** 200
***Post parameters :*** username=xxxx&password=xxxx
***Response:*** ‹ •UmoÓ0þÞ_aR!©YÓn¬Í"¡2H›?ø„.ñ%1sâ`;[â¿syiצÍ`NU;ñùîžçÎwþ‹wŸ—·ß¾\³Ôf2ø› ?F÷ÂJfžÇ\vƒú5»ÖZiÜì4RZ`QÚ ½rJ»oœvËØõF¬•ö{ûZ?¢»D«2çs–«™ÆÁ2i%%óè^×c±=÷g°]†Š¯;3ЉÈçlòºXÑéɬX-öb•[7†LÈõœ9ŸÊHp`4ä?»…Te0boµ ......
***Content Encoding:*** gzip
***Content Length:*** 761
***Content Type:*** text/html
***Content:*** sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@7c4ccb2f
即使使用POST方法,我也会得到相同的响应。 谁能告诉我我要去哪里错或需要纠正什么。
提前致谢。
更新:根据@Hendrik Simon的回复,我可以以可读的格式获取回复。 但是现在我面临另一个障碍。 现在的响应是
Response: <!DOCTYPE html><html><head> <title>500 - Server Error</title> <meta charset="utf-8"> <style> html { background: none repeat scroll 0 0 #EEEEEE; } body { margin: 17px 0 15px; font-family: "Lucida Grande", Tahoma, Arial, Verdana, sans-serif; padding: 0; text-align: center; font-size: small; } #content { display: block; margin: 20px 14px 10px; padding: 0 0 20px; position: relative; } #container { background: none repeat scroll 0 0 #FFFFFF; border: 1px solid #CCCCCC; box-shadow: 0 0 5px rgba(0, 0, 0, 0.2); text-align: left; margin: 0 auto; width: 640px; min-height: 200px; } h2 { font-size: 160%; clear: left; font-weight: 400; margin: 0 0 5px; text-align: left; } #content div { color: #555555; font-size: 95%; line-height: 135%; } #logo { background-color: #003C69; background-image: none; margin: 0; min-height: 0; padding: 0; text-decoration: none; text-indent: -9000px; } #logo a { background-repeat: no-repeat; background-image: url("cybersource_logo.gif"); height: 55px; min-height: 0; display: block; overflow: hidden; } </style></head><body><div id="container"> <h1 id="logo"><a>CyberSource</a></h1> <div id="content"> <h2>The server encountered an error and could not complete your request.</h2> <br/> <div>If the problem persists please report your problem.</div> </div></div><script src="ntpagetag.js"></script></body></html>
当我在浏览器或REST客户端上使用相同的URL时,通常会弹出一个窗口以输入凭据。 成功提交后,我得到了所需的数据。 谁能指导我如何使用JAVA代码处理这种情况?
好像响应是gzip编码的,您不必解压缩它。
尝试删除以下行
con.setRequestProperty("Accept-Encoding", "gzip, deflate, sdch");
更新:
正如@RealSkeptic正确提到的,如果它是GET
-Request,则无法将数据写入连接。 使用GET-Request,您必须像这样将参数添加到URL:
http://...?param1=bla¶m2=blubb
我认为,只要您调用con.setDoOutput(true);
无论您设置为哪种请求方法,您的连接都会自动执行POST请求。 因此,您永远不会在示例中发出GET-Request。
例如,当您使用Wireshark分析流量时,您可以轻松地看到这一点。
对于服务器错误,不进行重现就很难分析。 至少对于我来说,在不知道URL的情况下重新生成它是不可能的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.