Сценарий: Мне нужно нажать URL HTTPS, чтобы извлечь CSV-файл или его содержимое. Я могу успешно получить нужное содержимое с помощью расширенного клиентского плагина REST в браузере Chrome. Но получайте значение мусора, пытаясь сделать то же самое с помощью java-кода.Ответ от HttpURLConnection в 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:*** [email protected]cb2f
я получаю тот же ответ, даже с помощью метода 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>
Когда я использую тот же URL-адрес в браузере или REST клиент я обычно получаю всплывающее окно для ввода учетных данных. При успешном отправке я получаю нужные данные. Может ли кто-нибудь помочь мне справиться с этим сценарием с использованием кода JAVA?
Две вещи, которые выделяются, это то, что вы пытаетесь отправить тело запроса в запрос GET. Предполагается, что параметры в GET находятся в URL-адресе. И в запросе POST тип содержимого должен, вероятно, * not * быть текстовым/обычным, но это может зависеть от определения конкретной службы. Однако вы должны отправлять контентную длину. – RealSkeptic
Спасибо за ваш ответ @RealSkeptic. Теперь он работает в ответ Хендрика Саймона. Можете ли вы мне помочь? – VikramV