當前位置: 首頁>>代碼示例>>Java>>正文


Java MockWebServer.getPort方法代碼示例

本文整理匯總了Java中com.squareup.okhttp.mockwebserver.MockWebServer.getPort方法的典型用法代碼示例。如果您正苦於以下問題:Java MockWebServer.getPort方法的具體用法?Java MockWebServer.getPort怎麽用?Java MockWebServer.getPort使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.squareup.okhttp.mockwebserver.MockWebServer的用法示例。


在下文中一共展示了MockWebServer.getPort方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: redirectToAnotherOriginServer

import com.squareup.okhttp.mockwebserver.MockWebServer; //導入方法依賴的package包/類
private void redirectToAnotherOriginServer(boolean https) throws Exception {
  server2 = new MockWebServer();
  if (https) {
    server.useHttps(sslContext.getSocketFactory(), false);
    server2.useHttps(sslContext.getSocketFactory(), false);
    server2.setNpnEnabled(false);
    client.setSslSocketFactory(sslContext.getSocketFactory());
    client.setHostnameVerifier(new RecordingHostnameVerifier());
  }

  server2.enqueue(new MockResponse().setBody("This is the 2nd server!"));
  server2.enqueue(new MockResponse().setBody("This is the 2nd server, again!"));
  server2.play();

  server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)
      .addHeader("Location: " + server2.getUrl("/").toString())
      .setBody("This page has moved!"));
  server.enqueue(new MockResponse().setBody("This is the first server again!"));
  server.play();

  connection = client.open(server.getUrl("/"));
  assertContent("This is the 2nd server!", connection);
  assertEquals(server2.getUrl("/"), connection.getURL());

  // make sure the first server was careful to recycle the connection
  assertContent("This is the first server again!", client.open(server.getUrl("/")));
  assertContent("This is the 2nd server, again!", client.open(server2.getUrl("/")));

  String server1Host = hostName + ":" + server.getPort();
  String server2Host = hostName + ":" + server2.getPort();
  assertContains(server.takeRequest().getHeaders(), "Host: " + server1Host);
  assertContains(server2.takeRequest().getHeaders(), "Host: " + server2Host);
  assertEquals("Expected connection reuse", 1, server.takeRequest().getSequenceNumber());
  assertEquals("Expected connection reuse", 1, server2.takeRequest().getSequenceNumber());
}
 
開發者ID:xin3liang,項目名稱:platform_external_okhttp,代碼行數:36,代碼來源:URLConnectionTest.java

示例2: setUp

import com.squareup.okhttp.mockwebserver.MockWebServer; //導入方法依賴的package包/類
private void setUp(int poolSize) throws Exception {
  SocketFactory socketFactory = SocketFactory.getDefault();

  spdyServer = new MockWebServer();
  httpServer = new MockWebServer();
  spdyServer.useHttps(sslContext.getSocketFactory(), false);

  httpServer.play();
  httpAddress = new Address(httpServer.getHostName(), httpServer.getPort(), socketFactory, null,
      null, HttpAuthenticator.SYSTEM_DEFAULT, null, Protocol.SPDY3_AND_HTTP11);
  httpSocketAddress = new InetSocketAddress(InetAddress.getByName(httpServer.getHostName()),
      httpServer.getPort());

  spdyServer.play();
  spdyAddress = new Address(spdyServer.getHostName(), spdyServer.getPort(), socketFactory,
      sslContext.getSocketFactory(), new RecordingHostnameVerifier(),
      HttpAuthenticator.SYSTEM_DEFAULT, null,Protocol.SPDY3_AND_HTTP11);
  spdySocketAddress = new InetSocketAddress(InetAddress.getByName(spdyServer.getHostName()),
      spdyServer.getPort());

  Route httpRoute = new Route(httpAddress, Proxy.NO_PROXY, httpSocketAddress, true);
  Route spdyRoute = new Route(spdyAddress, Proxy.NO_PROXY, spdySocketAddress, true);
  pool = new ConnectionPool(poolSize, KEEP_ALIVE_DURATION_MS);
  httpA = new Connection(pool, httpRoute);
  httpA.connect(200, 200, null);
  httpB = new Connection(pool, httpRoute);
  httpB.connect(200, 200, null);
  httpC = new Connection(pool, httpRoute);
  httpC.connect(200, 200, null);
  httpD = new Connection(pool, httpRoute);
  httpD.connect(200, 200, null);
  httpE = new Connection(pool, httpRoute);
  httpE.connect(200, 200, null);
  spdyA = new Connection(pool, spdyRoute);
  spdyA.connect(20000, 20000, null);

  owner = new Object();
  httpA.setOwner(owner);
  httpB.setOwner(owner);
  httpC.setOwner(owner);
  httpD.setOwner(owner);
  httpE.setOwner(owner);
}
 
開發者ID:xin3liang,項目名稱:platform_external_okhttp,代碼行數:44,代碼來源:ConnectionPoolTest.java


注:本文中的com.squareup.okhttp.mockwebserver.MockWebServer.getPort方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。