Package org.lilystudio.httpclient

Examples of org.lilystudio.httpclient.HttpClient


      if (len > 0) {
        s.setLength(len - 1);
      }
      httpMethod.setRequestHeader("Cookie", s.toString());
    }
    HttpClient httpClient = new HttpClient();
    httpClient.setAutoDecode(true);
    try {
      if (param != null) {
        if (!isGet) {
          PostMethod method = (PostMethod) httpMethod;
          int size = param.size();
          for (int i = 0; i < size; i++) {
            Parameter item = param.get(i);
            Object value = item.getValue(relay);
            method.addRequestBody(item.name, value != null ? URLDecoder.decode(
                value.toString(), encoding) : "");
          }
        }
      }
      while (true) {
        // 计算是否需要跳转
        int statusCode = 0;
        statusCode = httpClient.execute(httpMethod);
        if (statusCode == HttpServletResponse.SC_MOVED_PERMANENTLY
            || statusCode == HttpServletResponse.SC_MOVED_TEMPORARILY) {
          String locationHeader = httpClient.getResponseHeader("Location");
          if (locationHeader != null) {
            httpMethod = new GetMethod(locationHeader);
            continue;
          }
        }
        response.setStatus(statusCode);
        break;
      }

      // 将得到的结果的头部输出
      Map<String, String> headers = httpClient.getResponseHeaders();
      for (Map.Entry<String, String> header : headers.entrySet()) {
        String name = header.getKey();
        String value = header.getValue();
        if (name.equalsIgnoreCase("Set-Cookie")) {
          // 如果返回的cookie中有jsessionid, 为防止冲突,
          // 保存在用户的专属信息中
          int index = value.indexOf("JSESSIONID");
          if (index >= 0) {
            int len = value.length();
            int endIndex = (value.indexOf(';', index + 10) + len) % len + 1;
            String sessionId = value.substring(index, endIndex);
            value = value.substring(0, index) + value.substring(endIndex);
            response.setHeader(name, value);
            UserInformation info = relay.getUserInformation(true);
            info.setProperty("_JSESSIONID", sessionId);
            continue;
          }
        }
        response.setHeader(name, value);
      }

      // 将得到的结果输出
      InputStream in = httpClient.getResponseBodyAsStream();
      if (in != null) {
        OutputStream out = response.getOutputStream();
        byte[] buf = new byte[1024];
        while (true) {
          int len = in.read(buf);
          if (len < 0) {
            break;
          }
          out.write(buf, 0, len);
        }
        out.flush();
      }
    } finally {
      httpClient.close();
    }
  }
View Full Code Here

TOP

Related Classes of org.lilystudio.httpclient.HttpClient

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.