Beispiel #1
0
 // set up connection defaults, and details from request
 private static HttpURLConnection createConnection(Connection.Request req) throws IOException {
   HttpURLConnection conn = (HttpURLConnection) req.url().openConnection();
   conn.setRequestMethod(req.method().name());
   conn.setInstanceFollowRedirects(false); // don't rely on native redirection support
   conn.setConnectTimeout(req.timeout());
   conn.setReadTimeout(req.timeout());
   if (req.method() == Method.POST) conn.setDoOutput(true);
   if (req.cookies().size() > 0) conn.addRequestProperty("Cookie", getRequestCookieString(req));
   for (Map.Entry<String, String> header : req.headers().entrySet()) {
     conn.addRequestProperty(header.getKey(), header.getValue());
   }
   return conn;
 }
Beispiel #2
0
 public Connection method(Method method) {
   req.method(method);
   return this;
 }
Beispiel #3
0
    static Response execute(Connection.Request req, Response previousResponse) throws IOException {
      Validate.notNull(req, "Request must not be null");
      String protocol = req.url().getProtocol();
      Validate.isTrue(
          protocol.equals("http") || protocol.equals("https"),
          "Only http & https protocols supported");

      // set up the request for execution
      if (req.method() == Connection.Method.GET && req.data().size() > 0)
        serialiseRequestUrl(req); // appends query string
      HttpURLConnection conn = createConnection(req);
      conn.connect();
      if (req.method() == Connection.Method.POST) writePost(req.data(), conn.getOutputStream());

      int status = conn.getResponseCode();
      boolean needsRedirect = false;
      if (status != HttpURLConnection.HTTP_OK) {
        if (status == HttpURLConnection.HTTP_MOVED_TEMP
            || status == HttpURLConnection.HTTP_MOVED_PERM
            || status == HttpURLConnection.HTTP_SEE_OTHER) needsRedirect = true;
        else if (!req.ignoreHttpErrors())
          throw new IOException(status + " error loading URL " + req.url().toString());
      }
      Response res = new Response(previousResponse);
      res.setupFromConnection(conn, previousResponse);
      if (needsRedirect && req.followRedirects()) {
        req.method(
            Method
                .GET); // always redirect with a get. any data param from original req are dropped.
        req.data().clear();
        req.url(new URL(req.url(), res.header("Location")));
        for (Map.Entry<String, String> cookie :
            res.cookies.entrySet()) { // add response cookies to request (for e.g. login posts)
          req.cookie(cookie.getKey(), cookie.getValue());
        }
        return execute(req, res);
      }
      res.req = req;

      InputStream bodyStream = null;
      InputStream dataStream = null;
      try {
        dataStream = conn.getErrorStream() != null ? conn.getErrorStream() : conn.getInputStream();
        bodyStream =
            res.hasHeader("Content-Encoding")
                    && res.header("Content-Encoding").equalsIgnoreCase("gzip")
                ? new BufferedInputStream(new GZIPInputStream(dataStream))
                : new BufferedInputStream(dataStream);

        res.byteData = DataUtil.readToByteBuffer(bodyStream);
        res.charset =
            DataUtil.getCharsetFromContentType(
                res.contentType); // may be null, readInputStream deals with it
      } finally {
        if (bodyStream != null) bodyStream.close();
        if (dataStream != null) dataStream.close();
      }

      res.executed = true;
      return res;
    }
Beispiel #4
0
 public Document post() throws IOException {
   req.method(Method.POST);
   execute();
   return res.parse();
 }