com.squareup.okhttp.Protocol Java Examples

The following examples show how to use com.squareup.okhttp.Protocol. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OkHttpClientFactory.java    From Auth0.Android with MIT License 6 votes vote down vote up
@VisibleForTesting
OkHttpClient modifyClient(OkHttpClient client, boolean loggingEnabled, boolean tls12Enforced, int connectTimeout, int readTimeout, int writeTimeout) {
    if (loggingEnabled) {
        enableLogging(client);
    }
    if (tls12Enforced) {
        enforceTls12(client);
    }
    if(connectTimeout > 0){
        client.setConnectTimeout(connectTimeout, TimeUnit.SECONDS);
    }
    if(readTimeout > 0){
        client.setReadTimeout(readTimeout, TimeUnit.SECONDS);
    }
    if(writeTimeout > 0){
        client.setWriteTimeout(writeTimeout, TimeUnit.SECONDS);
    }
    client.setProtocols(Arrays.asList(Protocol.HTTP_1_1, Protocol.SPDY_3));
    return client;
}
 
Example #2
Source File: LoboBrowser.java    From LoboBrowser with MIT License 6 votes vote down vote up
/**
 * Initializes the global URLStreamHandlerFactory.
 * <p>
 * This method is invoked by {@link #init(boolean, boolean)}.
 */
public static void initProtocols(final SSLSocketFactory sslSocketFactory) {
  // Configure URL protocol handlers
  final StreamHandlerFactory factory = StreamHandlerFactory.getInstance();
  URL.setURLStreamHandlerFactory(factory);
  final OkHttpClient okHttpClient = new OkHttpClient();

  final ArrayList<Protocol> protocolList = new ArrayList<>(2);
  protocolList.add(Protocol.HTTP_1_1);
  protocolList.add(Protocol.HTTP_2);
  okHttpClient.setProtocols(protocolList);

  okHttpClient.setConnectTimeout(100, TimeUnit.SECONDS);

  // HttpsURLConnection.setDefaultSSLSocketFactory(sslSocketFactory);
  okHttpClient.setSslSocketFactory(sslSocketFactory);
  okHttpClient.setFollowRedirects(false);
  okHttpClient.setFollowSslRedirects(false);
  factory.addFactory(new OkUrlFactory(okHttpClient));
  factory.addFactory(new LocalStreamHandlerFactory());
}
 
Example #3
Source File: HttpConnectorFactory.java    From apiman with Apache License 2.0 6 votes vote down vote up
/**
 * @return a new http client
 */
private OkHttpClient createHttpClient() {
    OkHttpClient client = new OkHttpClient();
    client.setReadTimeout(connectorOptions.getReadTimeout(), TimeUnit.SECONDS);
    client.setWriteTimeout(connectorOptions.getWriteTimeout(), TimeUnit.SECONDS);
    client.setConnectTimeout(connectorOptions.getConnectTimeout(), TimeUnit.SECONDS);
    client.setFollowRedirects(connectorOptions.isFollowRedirects());
    client.setFollowSslRedirects(connectorOptions.isFollowRedirects());
    client.setProxySelector(ProxySelector.getDefault());
    client.setCookieHandler(CookieHandler.getDefault());
    client.setCertificatePinner(CertificatePinner.DEFAULT);
    client.setAuthenticator(AuthenticatorAdapter.INSTANCE);
    client.setConnectionPool(ConnectionPool.getDefault());
    client.setProtocols(Util.immutableList(Protocol.HTTP_1_1));
    client.setConnectionSpecs(DEFAULT_CONNECTION_SPECS);
    client.setSocketFactory(SocketFactory.getDefault());
    Internal.instance.setNetwork(client, Network.DEFAULT);

    return client;
}
 
Example #4
Source File: HawkularMetricsClient.java    From apiman with Apache License 2.0 6 votes vote down vote up
/**
 * Constructor.
 * @param metricsServer
 */
public HawkularMetricsClient(URL metricsServer) {
    this.serverUrl = metricsServer;
    httpClient = new OkHttpClient();
    httpClient.setReadTimeout(DEFAULT_READ_TIMEOUT, TimeUnit.SECONDS);
    httpClient.setWriteTimeout(DEFAULT_WRITE_TIMEOUT, TimeUnit.SECONDS);
    httpClient.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT, TimeUnit.SECONDS);
    httpClient.setFollowRedirects(true);
    httpClient.setFollowSslRedirects(true);
    httpClient.setProxySelector(ProxySelector.getDefault());
    httpClient.setCookieHandler(CookieHandler.getDefault());
    httpClient.setCertificatePinner(CertificatePinner.DEFAULT);
    httpClient.setAuthenticator(AuthenticatorAdapter.INSTANCE);
    httpClient.setConnectionPool(ConnectionPool.getDefault());
    httpClient.setProtocols(Util.immutableList(Protocol.HTTP_1_1));
    httpClient.setConnectionSpecs(DEFAULT_CONNECTION_SPECS);
    httpClient.setSocketFactory(SocketFactory.getDefault());
    Internal.instance.setNetwork(httpClient, Network.DEFAULT);
}
 
Example #5
Source File: MattermostService.java    From mattermost-android-classic with Apache License 2.0 5 votes vote down vote up
public MattermostService(Context context) {
    this.context = context;
    String userAgent = context.getResources().getString(R.string.app_user_agent);

    cookieStore = new WebkitCookieManagerProxy();

    client.setProtocols(Arrays.asList(Protocol.HTTP_1_1));
    client.setCookieHandler(cookieStore);
    preferences = context.getSharedPreferences("App", Context.MODE_PRIVATE);
}
 
Example #6
Source File: OkHttpClientFactoryTest.java    From Auth0.Android with MIT License 5 votes vote down vote up
@Test
public void shouldNotUseHttp2Protocol() {
    OkHttpClient client = factory.createClient(false, false, 0, 0, 0);
    //Doesn't use default protocols
    assertThat(client.getProtocols(), is(notNullValue()));
    assertThat(client.getProtocols().contains(Protocol.HTTP_1_1), is(true));
    assertThat(client.getProtocols().contains(Protocol.SPDY_3), is(true));
    assertThat(client.getProtocols().contains(Protocol.HTTP_2), is(false));
}
 
Example #7
Source File: BaseRequestTest.java    From Auth0.Android with MIT License 5 votes vote down vote up
private Response createJsonResponse(String jsonPayload, int code) {
    Request request = new Request.Builder()
            .url("https://someurl.com")
            .build();

    final ResponseBody responseBody = ResponseBody.create(MediaType.parse("application/json; charset=utf-8"), jsonPayload);
    return new Response.Builder()
            .request(request)
            .protocol(Protocol.HTTP_1_1)
            .body(responseBody)
            .code(code)
            .build();
}
 
Example #8
Source File: BaseRequestTest.java    From Auth0.Android with MIT License 5 votes vote down vote up
private Response createBytesResponse(byte[] content, int code) {
    Request request = new Request.Builder()
            .url("https://someurl.com")
            .build();

    final ResponseBody responseBody = ResponseBody.create(MediaType.parse("application/octet-stream; charset=utf-8"), content);
    return new Response.Builder()
            .request(request)
            .protocol(Protocol.HTTP_1_1)
            .body(responseBody)
            .code(code)
            .build();
}
 
Example #9
Source File: OkHttpStack.java    From SimplifyReader with Apache License 2.0 5 votes vote down vote up
private static ProtocolVersion parseProtocol(final Protocol p) {
    switch (p) {
        case HTTP_1_0:
            return new ProtocolVersion("HTTP", 1, 0);
        case HTTP_1_1:
            return new ProtocolVersion("HTTP", 1, 1);
        case SPDY_3:
            return new ProtocolVersion("SPDY", 3, 1);
        case HTTP_2:
            return new ProtocolVersion("HTTP", 2, 0);
    }

    throw new IllegalAccessError("Unkwown protocol");
}
 
Example #10
Source File: OkHttpStack.java    From wasp with Apache License 2.0 5 votes vote down vote up
private static ProtocolVersion parseProtocol(final Protocol protocol) {
  switch (protocol) {
    case HTTP_1_0:
      return new ProtocolVersion("HTTP", 1, 0);
    case HTTP_1_1:
      return new ProtocolVersion("HTTP", 1, 1);
    case SPDY_3:
      return new ProtocolVersion("SPDY", 3, 1);
    case HTTP_2:
      return new ProtocolVersion("HTTP", 2, 0);
    default:
      throw new IllegalAccessError("Unkwown protocol");
  }
}
 
Example #11
Source File: HttpProtocolSender.java    From jesos with Apache License 2.0 5 votes vote down vote up
public HttpProtocolSender(final UPID sender)
{
    this.client = new OkHttpClient();
    client.setProtocols(ImmutableList.of(Protocol.HTTP_1_1));

    this.sender = sender.asString();
}
 
Example #12
Source File: OkHttpStack.java    From CrossBow with Apache License 2.0 5 votes vote down vote up
private static ProtocolVersion parseProtocol(final Protocol p) {
    switch (p) {
        case HTTP_1_0:
            return new ProtocolVersion("HTTP", 1, 0);
        case HTTP_1_1:
            return new ProtocolVersion("HTTP", 1, 1);
        case SPDY_3:
            return new ProtocolVersion("SPDY", 3, 1);
        case HTTP_2:
            return new ProtocolVersion("HTTP", 2, 0);
    }

    throw new IllegalAccessError("Unkwown protocol");
}
 
Example #13
Source File: HttpURLConnectionImpl.java    From apiman with Apache License 2.0 5 votes vote down vote up
private void setProtocols(String protocolsString, boolean append) {
  List<Protocol> protocolsList = new ArrayList<>();
  if (append) {
    protocolsList.addAll(client.getProtocols());
  }
  for (String protocol : protocolsString.split(",", -1)) {
    try {
      protocolsList.add(Protocol.get(protocol));
    } catch (IOException e) {
      throw new IllegalStateException(e);
    }
  }
  client.setProtocols(protocolsList);
}
 
Example #14
Source File: StethoInterceptorTest.java    From stetho with MIT License 4 votes vote down vote up
@Test
public void testHappyPath() throws IOException {
  InOrder inOrder = Mockito.inOrder(mMockEventReporter);
  hookAlmostRealRequestWillBeSent(mMockEventReporter);
  ByteArrayOutputStream capturedOutput =
      hookAlmostRealInterpretResponseStream(mMockEventReporter);

  Uri requestUri = Uri.parse("http://www.facebook.com/nowhere");
  String requestText = "Test input";
  Request request = new Request.Builder()
      .url(requestUri.toString())
      .method(
          "POST",
          RequestBody.create(MediaType.parse("text/plain"), requestText))
      .build();
  String originalBodyData = "Success!";
  Response reply = new Response.Builder()
      .request(request)
      .protocol(Protocol.HTTP_1_1)
      .code(200)
      .body(ResponseBody.create(MediaType.parse("text/plain"), originalBodyData))
      .build();
  Response filteredResponse =
      mInterceptor.intercept(
          new SimpleTestChain(request, reply, mock(Connection.class)));

  inOrder.verify(mMockEventReporter).isEnabled();
  inOrder.verify(mMockEventReporter)
      .requestWillBeSent(any(NetworkEventReporter.InspectorRequest.class));
  inOrder.verify(mMockEventReporter)
      .dataSent(
          anyString(),
          eq(requestText.length()),
          eq(requestText.length()));
  inOrder.verify(mMockEventReporter)
      .responseHeadersReceived(any(NetworkEventReporter.InspectorResponse.class));

  String filteredResponseString = filteredResponse.body().string();
  String interceptedOutput = capturedOutput.toString();

  inOrder.verify(mMockEventReporter).dataReceived(anyString(), anyInt(), anyInt());
  inOrder.verify(mMockEventReporter).responseReadFinished(anyString());

  assertEquals(originalBodyData, filteredResponseString);
  assertEquals(originalBodyData, interceptedOutput);

  inOrder.verifyNoMoreInteractions();
}
 
Example #15
Source File: HttpClientTest.java    From zsync4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private Response fakeResponse(int code) {
  Request fakeRequest = new Request.Builder().url("http://host/url").build();
  return new Response.Builder().protocol(Protocol.HTTP_2).request(fakeRequest).code(code).build();
}