Ejemplo n.º 1
0
  private PostMethod convertHttpServletRequestToPostMethod(String url, HttpServletRequest request) {
    PostMethod postMethod = new PostMethod(url);

    for (Enumeration headers = request.getHeaderNames(); headers.hasMoreElements(); ) {
      String headerName = (String) headers.nextElement();
      String headerValue = (String) request.getHeader(headerName);
      postMethod.addRequestHeader(headerName, headerValue);
    }

    postMethod.removeRequestHeader("Host");
    postMethod.addRequestHeader("Host", request.getRequestURL().toString());

    for (Enumeration names = request.getParameterNames(); names.hasMoreElements(); ) {
      String paramName = (String) names.nextElement();
      String paramValue = (String) request.getParameter(paramName);
      postMethod.addParameter(paramName, paramValue);
    }

    StringBuilder requestBody = new StringBuilder();
    try {
      BufferedReader reader = request.getReader();
      String line;
      while (null != (line = reader.readLine())) {
        requestBody.append(line);
      }
      reader.close();
    } catch (IOException e) {
      requestBody.append("");
    }

    postMethod.setRequestEntity(new StringRequestEntity(requestBody.toString()));

    return postMethod;
  }
Ejemplo n.º 2
0
 // sends packet to server as a http post method
 private boolean sendPacketToServer(byte[] packet) {
   try {
     postMethod.removeRequestHeader("content-length");
     RequestEntity request = new ByteArrayRequestEntity(packet);
     postMethod.setRequestEntity(request);
     client.executeMethod(postMethod);
   } catch (IOException e) {
     return false;
   }
   return true;
 }