| 398 | } |
| 399 | |
| 400 | public byte[] toByteArray() throws Exception { |
| 401 | byte[] result = null; |
| 402 | byte[] newLine = new String("\r\n").getBytes(); |
| 403 | String statusLine = header.getStatusline(); |
| 404 | |
| 405 | if (flag_request) { |
| 406 | |
| 407 | // String query = (getQuery() != null && getQuery().length() > 0) ? |
| 408 | // "?"+URLEncoder.encode(getQuery(),"utf-8") : ""; |
| 409 | String query = (getQueryAsString() != null && getQueryAsString().length() > 0) |
| 410 | ? "?" + getQueryAsString() |
| 411 | : ""; |
| 412 | if (this.isProxy() && this.flag_disable_proxy_format_url == false) { |
| 413 | |
| 414 | String proxyPort = (getServerPort() > 0) ? ":" + String.valueOf(getServerPort()) : ""; |
| 415 | statusLine = String.format("%s http://%s%s%s%s %s", this.method, getServerName(), proxyPort, getPath(), |
| 416 | query, this.version); |
| 417 | } else { |
| 418 | |
| 419 | statusLine = String.format("%s %s%s %s", this.method, this.path, query, this.version); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | result = ArrayUtils.addAll(result, statusLine.getBytes()); |
| 424 | result = ArrayUtils.addAll(result, newLine); |
| 425 | if (!flag_request && this.statusCode != null && this.statusCode.equals("100")) { |
| 426 | |
| 427 | // 100 Continueの場合は、Content-Lengthがいらないのですぐに返す |
| 428 | result = ArrayUtils.addAll(result, newLine); |
| 429 | return result; |
| 430 | } |
| 431 | result = ArrayUtils.addAll(result, header.toByteArray()); |
| 432 | |
| 433 | if (body.length == 0 && flag_request && this.getHost() != null && (this.getHost().equals("dpoint.jp") |
| 434 | || this.getHost().equals("id.smt.docomo.ne.jp") || this.getHost().equals("cfg.smt.docomo.ne.jp"))) { |
| 435 | |
| 436 | // 特定サイトでは Content-Length: 0をつけるとうまく動かないので例外処理する |
| 437 | } else if (this.flag_disable_content_length) { |
| 438 | |
| 439 | // content-lengthがいらないと明示的に指定したケース |
| 440 | } else if (this.flag_dont_touch_content_length) { |
| 441 | |
| 442 | // content-lengthを触らないと明示的に指定したケース |
| 443 | } else { |
| 444 | |
| 445 | // Content-Typeがないパターンでも必ずContent-Lengthはつけるべき |
| 446 | // if (header.containsKey("Content-Type")) { |
| 447 | result = ArrayUtils.addAll(result, String.format("Content-Length: %d", body.length).getBytes()); |
| 448 | result = ArrayUtils.addAll(result, newLine); |
| 449 | // } |
| 450 | } |
| 451 | |
| 452 | result = ArrayUtils.addAll(result, newLine); |
| 453 | return ArrayUtils.addAll(result, body); |
| 454 | } |
| 455 | |
| 456 | public HttpHeader getOriginalHeader() { |
| 457 | return originalHeader; |