|
22 | 22 | import java.net.URL; |
23 | 23 | import java.util.HashMap; |
24 | 24 | import java.util.Iterator; |
| 25 | +import java.util.List; |
25 | 26 | import java.util.Map; |
26 | 27 | import java.util.regex.Matcher; |
27 | 28 | import java.util.regex.Pattern; |
@@ -281,7 +282,27 @@ public String getAllResponseHeaders() { |
281 | 282 | * @return String the response header value. |
282 | 283 | */ |
283 | 284 | public String getResponseHeader(String key) { |
284 | | - return connection.getHeaderField(key); |
| 285 | + Map<String, List<String>> headerFields = connection.getHeaderFields(); |
| 286 | + List<String> list = headerFields.get(key); |
| 287 | + if (list == null) { |
| 288 | + return null; |
| 289 | + } |
| 290 | + if (list.size() == 0) { |
| 291 | + return ""; |
| 292 | + } |
| 293 | + String headerValue = null; |
| 294 | + for (Iterator<String> itr = list.iterator(); itr.hasNext();) { |
| 295 | + String value = (String) itr.next(); |
| 296 | + if (value != null) { |
| 297 | + if (headerValue == null) { |
| 298 | + headerValue = value; |
| 299 | + } else { |
| 300 | + headerValue = value + "\r\n" + headerValue; |
| 301 | + } |
| 302 | + } |
| 303 | + } |
| 304 | + return headerValue; // may have multiple Set-Cookie headers |
| 305 | + // return connection.getHeaderField(key); |
285 | 306 | } |
286 | 307 | /** |
287 | 308 | * Open connection for HTTP request with given method and URL |
@@ -407,10 +428,12 @@ private void request() { |
407 | 428 | } else { |
408 | 429 | connection.setReadTimeout(30000); // 30s |
409 | 430 | } |
| 431 | + connection.setInstanceFollowRedirects(false); |
410 | 432 | connection.setDoInput(true); |
411 | 433 | connection.setRequestMethod(method); |
412 | 434 | connection.setRequestProperty("User-Agent", |
413 | | - "Mozilla/5.0 (compatible; Java2Script/2.0.0)"); |
| 435 | + // "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 GTB5 (Java2Script/2.0.0)"); |
| 436 | + "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 GTB5"); |
414 | 437 | if ("post".equalsIgnoreCase(method)) { |
415 | 438 | connection.setDoOutput(true); |
416 | 439 | connection.setRequestProperty("Content-Type", |
@@ -471,8 +494,8 @@ private void request() { |
471 | 494 |
|
472 | 495 | receiving = initializeReceivingMonitor(); |
473 | 496 |
|
474 | | - ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
475 | | - byte[] buffer = new byte[1024]; |
| 497 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(10240); |
| 498 | + byte[] buffer = new byte[10240]; |
476 | 499 | int read; |
477 | 500 | while (!toAbort && (read = is.read(buffer)) != -1) { |
478 | 501 | if (checkAbort()) return; // stop receiving anything |
|
0 commit comments