public Socket connectSocket(
      Socket sock,
      String host,
      int port,
      InetAddress localAddress,
      int localPort,
      HttpParams params)
      throws IOException, UnknownHostException, ConnectTimeoutException {
    int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
    int soTimeout = HttpConnectionParams.getSoTimeout(params);

    InetSocketAddress remoteAddress = new InetSocketAddress(host, port);
    SSLSocket sslsock = (SSLSocket) ((sock != null) ? sock : createSocket());

    if ((localAddress != null) || (localPort > 0)) {
      if (localPort < 0) {
        localPort = 0;
      }
      InetSocketAddress isa = new InetSocketAddress(localAddress, localPort);
      sslsock.bind(isa);
    }

    sslsock.connect(remoteAddress, connTimeout);
    sslsock.setSoTimeout(soTimeout);
    return sslsock;
  }
  public Socket connectSocket(
      Socket sock,
      String host,
      int port,
      InetAddress localAddress,
      int localPort,
      HttpParams params)
      throws IOException {
    final int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
    final int soTimeout = HttpConnectionParams.getSoTimeout(params);

    final InetSocketAddress remoteAddress = new InetSocketAddress(host, port);
    final SSLSocket sslsock = (SSLSocket) ((sock != null) ? sock : createSocket());

    if ((localAddress != null) || (localPort > 0)) {
      // we need to bind explicitly
      if (localPort < 0) {
        localPort = 0; // indicates "any"
      }
      final InetSocketAddress isa = new InetSocketAddress(localAddress, localPort);
      sslsock.bind(isa);
    }

    sslsock.connect(remoteAddress, connTimeout);
    sslsock.setSoTimeout(soTimeout);

    return sslsock;
  }
Example #3
0
  public HttpResponse emulateRequest(
      HttpHost httpHost,
      HttpRequest httpRequest,
      HttpContext httpContext,
      RequestDirector requestDirector)
      throws HttpException, IOException {
    if (logHttpRequests) {
      System.out.println("  <-- " + httpRequest.getRequestLine());
    }
    HttpResponse httpResponse = findResponse(httpRequest);
    if (logHttpRequests) {
      System.out.println(
          "  --> " + (httpResponse == null ? null : httpResponse.getStatusLine().getStatusCode()));
    }

    if (httpResponse == null) {
      throw new RuntimeException(
          "Unexpected call to execute, no pending responses are available. See Robolectric.addPendingResponse(). Request was: "
              + httpRequest.getRequestLine().getMethod()
              + " "
              + httpRequest.getRequestLine().getUri());
    } else {
      HttpParams params = httpResponse.getParams();

      if (HttpConnectionParams.getConnectionTimeout(params) < 0) {
        throw new ConnectTimeoutException("Socket is not connected");
      } else if (HttpConnectionParams.getSoTimeout(params) < 0) {
        throw new ConnectTimeoutException("The operation timed out");
      }
    }

    addRequestInfo(new HttpRequestInfo(httpRequest, httpHost, httpContext, requestDirector));
    addHttpResponse(httpResponse);
    return httpResponse;
  }
Example #4
0
  @Override
  public Socket connectSocket(
      Socket sock,
      String host,
      int port,
      InetAddress localAddress,
      int localPort,
      HttpParams params)
      throws IOException, UnknownHostException, ConnectTimeoutException {
    if (host == null) {
      throw new IllegalArgumentException("Target host may not be null.");
    }
    if (params == null) {
      throw new IllegalArgumentException("Parameters may not be null.");
    }

    if (App.DEBUG)
      Log.e(
          TAG + "delete",
          "ConnectSocket with "
              + "\n\t host="
              + host
              + "\n\t port="
              + port
              + "\n\t localport="
              + localPort);

    SSLSocket sslsock = (SSLSocket) ((sock != null) ? sock : createSocket());

    if ((localAddress != null) || (localPort > 0)) {
      if (localPort < 0) localPort = 0;

      InetSocketAddress isa = new InetSocketAddress(localAddress, localPort);
      sslsock.bind(isa);
    }

    int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
    int soTimeout = HttpConnectionParams.getSoTimeout(params);

    InetSocketAddress remoteAddress = new InetSocketAddress(host, port);

    sslsock.connect(remoteAddress, connTimeout);

    sslsock.setSoTimeout(soTimeout);

    try {
      hostnameVerifier.verify(host, sslsock);
    } catch (IOException iox) {
      try {
        sslsock.close();
      } catch (Exception x) {
      }

      throw iox;
    }

    return sslsock;
  }
  // non-javadoc, see interface org.apache.http.conn.SocketFactory
  public Socket connectSocket(
      final Socket sock,
      final String host,
      final int port,
      final InetAddress localAddress,
      int localPort,
      final HttpParams params)
      throws IOException {

    if (host == null) {
      throw new IllegalArgumentException("Target host may not be null.");
    }
    if (params == null) {
      throw new IllegalArgumentException("Parameters may not be null.");
    }

    SSLSocket sslsock = (SSLSocket) ((sock != null) ? sock : createSocket());

    if ((localAddress != null) || (localPort > 0)) {

      // we need to bind explicitly
      if (localPort < 0) localPort = 0; // indicates "any"

      InetSocketAddress isa = new InetSocketAddress(localAddress, localPort);
      sslsock.bind(isa);
    }

    int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
    int soTimeout = HttpConnectionParams.getSoTimeout(params);

    InetSocketAddress remoteAddress;
    if (this.nameResolver != null) {
      remoteAddress = new InetSocketAddress(this.nameResolver.resolve(host), port);
    } else {
      remoteAddress = new InetSocketAddress(host, port);
    }
    try {
      sslsock.connect(remoteAddress, connTimeout);
    } catch (SocketTimeoutException ex) {
      throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out");
    }
    sslsock.setSoTimeout(soTimeout);
    try {
      hostnameVerifier.verify(host, sslsock);
      // verifyHostName() didn't blowup - good!
    } catch (IOException iox) {
      // close the socket before re-throwing the exception
      try {
        sslsock.close();
      } catch (Exception x) {
        /*ignore*/
      }
      throw iox;
    }

    return sslsock;
  }
  /**
   * Attempts to connects the socket to any of the {@link InetAddress}es the given host name
   * resolves to. If connection to all addresses fail, the last I/O exception is propagated to the
   * caller.
   *
   * @param sock socket to connect to any of the given addresses
   * @param host Host name to connect to
   * @param port the port to connect to
   * @param localAddress local address
   * @param localPort local port
   * @param params HTTP parameters
   * @throws IOException if an error occurs during the connection
   * @throws SocketTimeoutException if timeout expires before connecting
   */
  public Socket connectSocket(
      Socket sock,
      String host,
      int port,
      InetAddress localAddress,
      int localPort,
      HttpParams params)
      throws IOException {

    if (host == null) {
      throw new IllegalArgumentException("Target host may not be null.");
    }
    if (params == null) {
      throw new IllegalArgumentException("Parameters may not be null.");
    }

    if (sock == null) sock = createSocket();

    if ((localAddress != null) || (localPort > 0)) {

      // we need to bind explicitly
      if (localPort < 0) localPort = 0; // indicates "any"

      InetSocketAddress isa = new InetSocketAddress(localAddress, localPort);
      sock.bind(isa);
    }

    int timeout = HttpConnectionParams.getConnectionTimeout(params);

    InetAddress[] inetadrs = InetAddress.getAllByName(host);
    List<InetAddress> addresses = new ArrayList<InetAddress>(inetadrs.length);
    addresses.addAll(Arrays.asList(inetadrs));
    Collections.shuffle(addresses);

    IOException lastEx = null;
    for (InetAddress remoteAddress : addresses) {
      try {
        sock.connect(new InetSocketAddress(remoteAddress, port), timeout);
        break;
      } catch (SocketTimeoutException ex) {
        throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out");
      } catch (IOException ex) {
        // create new socket
        sock = new Socket();
        // keep the last exception and retry
        lastEx = ex;
      }
    }
    if (lastEx != null) {
      throw lastEx;
    }
    return sock;
  } // connectSocket
  @Override
  public Socket connectSocket(
      Socket socket,
      String host,
      int port,
      InetAddress localAddress,
      int localPort,
      HttpParams params)
      throws IOException, UnknownHostException, ConnectTimeoutException {

    int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
    int soTimeout = HttpConnectionParams.getSoTimeout(params);

    SocketAddress socketAddress = new InetSocketAddress(localAddress, localPort);
    socket.bind(socketAddress);
    socket.connect(new InetSocketAddress(host, port), connTimeout);
    socket.setSoTimeout(soTimeout);

    return socket;
  }
  @Override
  public Socket connectSocket(
      Socket sock,
      String host,
      int port,
      InetAddress localAddress,
      int localPort,
      HttpParams params)
      throws IOException, UnknownHostException, ConnectTimeoutException {
    if (host == null) {
      throw new IllegalArgumentException("Target host may not be null.");
    }
    if (params == null) {
      throw new IllegalArgumentException("Parameters may not be null.");
    }

    SSLSocket sslsock = (SSLSocket) ((sock != null) ? sock : createSocket());

    if ((localAddress != null) || (localPort > 0)) {

      // we need to bind explicitly
      if (localPort < 0) {
        localPort = 0; // indicates "any"
      }

      InetSocketAddress isa = new InetSocketAddress(localAddress, localPort);
      sslsock.bind(isa);
    }

    int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
    int soTimeout = HttpConnectionParams.getSoTimeout(params);

    InetSocketAddress remoteAddress;
    remoteAddress = new InetSocketAddress(host, port);

    sslsock.connect(remoteAddress, connTimeout);

    sslsock.setSoTimeout(soTimeout);

    return sslsock;
  }
 @Override
 public Socket connectSocket(
     Socket sock,
     String host,
     int port,
     InetAddress localAddress,
     int localPort,
     HttpParams params)
     throws IOException {
   SSLSocket sslSocket = (SSLSocket) ((sock != null) ? sock : createSocket());
   if ((localAddress != null) || (localPort > 0)) {
     if (localPort < 0) {
       localPort = 0;
     }
     sslSocket.bind(new InetSocketAddress(localAddress, localPort));
   }
   sslSocket.connect(
       new InetSocketAddress(host, port), HttpConnectionParams.getConnectionTimeout(params));
   sslSocket.setSoTimeout(HttpConnectionParams.getSoTimeout(params));
   return sslSocket;
 }