本文整理汇总了Java中com.squareup.okhttp.Protocol类的典型用法代码示例。如果您正苦于以下问题:Java Protocol类的具体用法?Java Protocol怎么用?Java Protocol使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Protocol类属于com.squareup.okhttp包,在下文中一共展示了Protocol类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeRequestHeaders
import com.squareup.okhttp.Protocol; //导入依赖的package包/类
public void writeRequestHeaders(Request request) throws IOException {
if (this.stream == null) {
List<Header> requestHeaders;
this.httpEngine.writingRequestHeaders();
boolean permitsRequestBody = this.httpEngine.permitsRequestBody(request);
if (this.framedConnection.getProtocol() == Protocol.HTTP_2) {
requestHeaders = http2HeadersList(request);
} else {
requestHeaders = spdy3HeadersList(request);
}
this.stream = this.framedConnection.newStream(requestHeaders, permitsRequestBody, true);
this.stream.readTimeout().timeout((long) this.httpEngine.client.getReadTimeout(),
TimeUnit.MILLISECONDS);
this.stream.writeTimeout().timeout((long) this.httpEngine.client.getWriteTimeout(),
TimeUnit.MILLISECONDS);
}
}
示例2: readHttp2HeadersList
import com.squareup.okhttp.Protocol; //导入依赖的package包/类
public static Response$Builder readHttp2HeadersList(List<Header> headerBlock) throws
IOException {
String status = null;
Builder headersBuilder = new Builder();
int size = headerBlock.size();
for (int i = 0; i < size; i++) {
ByteString name = ((Header) headerBlock.get(i)).name;
String value = ((Header) headerBlock.get(i)).value.utf8();
if (name.equals(Header.RESPONSE_STATUS)) {
status = value;
} else if (!HTTP_2_SKIPPED_RESPONSE_HEADERS.contains(name)) {
headersBuilder.add(name.utf8(), value);
}
}
if (status == null) {
throw new ProtocolException("Expected ':status' header not present");
}
StatusLine statusLine = StatusLine.parse("HTTP/1.1 " + status);
return new Response$Builder().protocol(Protocol.HTTP_2).code(statusLine.code).message
(statusLine.message).headers(headersBuilder.build());
}
示例3: setProtocols
import com.squareup.okhttp.Protocol; //导入依赖的package包/类
private void setProtocols(String protocolsString, boolean append) {
List<Protocol> protocolsList = new ArrayList();
if (append) {
protocolsList.addAll(this.client.getProtocols());
}
String[] split = protocolsString.split(",", -1);
int length = split.length;
int i = 0;
while (i < length) {
try {
protocolsList.add(Protocol.get(split[i]));
i++;
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
this.client.setProtocols(protocolsList);
}
示例4: connectSocket
import com.squareup.okhttp.Protocol; //导入依赖的package包/类
private void connectSocket(int connectTimeout, int readTimeout, int writeTimeout,
ConnectionSpecSelector connectionSpecSelector) throws IOException {
this.rawSocket.setSoTimeout(readTimeout);
try {
Platform.get().connectSocket(this.rawSocket, this.route.getSocketAddress(),
connectTimeout);
this.source = Okio.buffer(Okio.source(this.rawSocket));
this.sink = Okio.buffer(Okio.sink(this.rawSocket));
if (this.route.getAddress().getSslSocketFactory() != null) {
connectTls(readTimeout, writeTimeout, connectionSpecSelector);
} else {
this.protocol = Protocol.HTTP_1_1;
this.socket = this.rawSocket;
}
if (this.protocol == Protocol.SPDY_3 || this.protocol == Protocol.HTTP_2) {
this.socket.setSoTimeout(0);
FramedConnection framedConnection = new Builder(true).socket(this.socket, this
.route.getAddress().url().host(), this.source, this.sink).protocol(this
.protocol).build();
framedConnection.sendConnectionPreface();
this.framedConnection = framedConnection;
}
} catch (ConnectException e) {
throw new ConnectException("Failed to connect to " + this.route.getSocketAddress());
}
}
示例5: parseProtocol
import com.squareup.okhttp.Protocol; //导入依赖的package包/类
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");
}
示例6: parseProtocol
import com.squareup.okhttp.Protocol; //导入依赖的package包/类
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);
}
throw new IllegalAccessError("Unkwown protocol");
}
示例7: initProtocols
import com.squareup.okhttp.Protocol; //导入依赖的package包/类
/**
* 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 PlatformStreamHandlerFactory factory = PlatformStreamHandlerFactory.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());
}
示例8: get
import com.squareup.okhttp.Protocol; //导入依赖的package包/类
/**
* Returns the request status line, like "GET / HTTP/1.1". This is exposed
* to the application by {@link HttpURLConnection#getHeaderFields}, so it
* needs to be set even if the transport is SPDY.
*/
static String get(Request request, Proxy.Type proxyType, Protocol protocol) {
StringBuilder result = new StringBuilder();
result.append(request.method());
result.append(' ');
if (includeAuthorityInRequestLine(request, proxyType)) {
result.append(request.url());
} else {
result.append(requestPath(request.url()));
}
result.append(' ');
result.append(version(protocol));
return result.toString();
}
示例9: readResponse
import com.squareup.okhttp.Protocol; //导入依赖的package包/类
/** Parses bytes of a response header from an HTTP transport. */
public Response.Builder readResponse() throws IOException {
if (state != STATE_OPEN_REQUEST_BODY && state != STATE_READ_RESPONSE_HEADERS) {
throw new IllegalStateException("state: " + state);
}
while (true) {
String statusLineString = source.readUtf8LineStrict();
StatusLine statusLine = new StatusLine(statusLineString);
Response.Builder responseBuilder = new Response.Builder()
.statusLine(statusLine)
.header(OkHeaders.SELECTED_PROTOCOL, Protocol.HTTP_11.name.utf8());
Headers.Builder headersBuilder = new Headers.Builder();
readHeaders(headersBuilder);
responseBuilder.headers(headersBuilder.build());
if (statusLine.code() != HTTP_CONTINUE) {
state = STATE_OPEN_RESPONSE_BODY;
return responseBuilder;
}
}
}
示例10: parseNameValueBlock
import com.squareup.okhttp.Protocol; //导入依赖的package包/类
@Test public void parseNameValueBlock() throws IOException {
List<Header> headerBlock = headerEntries(
"cache-control", "no-cache, no-store",
"set-cookie", "Cookie1\u0000Cookie2",
":status", "200 OK",
":version", "HTTP/1.1");
Request request = new Request.Builder().url("http://square.com/").build();
Response response =
SpdyTransport.readNameValueBlock(headerBlock, Protocol.SPDY_3).request(request).build();
Headers headers = response.headers();
assertEquals(4, headers.size());
assertEquals("HTTP/1.1 200 OK", response.statusLine());
assertEquals("no-cache, no-store", headers.get("cache-control"));
assertEquals("Cookie2", headers.get("set-cookie"));
assertEquals(Protocol.SPDY_3.name.utf8(), headers.get(OkHeaders.SELECTED_PROTOCOL));
assertEquals(OkHeaders.SELECTED_PROTOCOL, headers.name(0));
assertEquals(Protocol.SPDY_3.name.utf8(), headers.value(0));
assertEquals("cache-control", headers.name(1));
assertEquals("no-cache, no-store", headers.value(1));
assertEquals("set-cookie", headers.name(2));
assertEquals("Cookie1", headers.value(2));
assertEquals("set-cookie", headers.name(3));
assertEquals("Cookie2", headers.value(3));
assertNull(headers.get(":status"));
assertNull(headers.get(":version"));
}
示例11: toNameValueBlock
import com.squareup.okhttp.Protocol; //导入依赖的package包/类
@Test public void toNameValueBlock() {
Request request = new Request.Builder()
.url("http://square.com/")
.header("cache-control", "no-cache, no-store")
.addHeader("set-cookie", "Cookie1")
.addHeader("set-cookie", "Cookie2")
.header(":status", "200 OK")
.build();
List<Header> headerBlock =
SpdyTransport.writeNameValueBlock(request, Protocol.SPDY_3, "HTTP/1.1");
List<Header> expected = headerEntries(
":method", "GET",
":path", "/",
":version", "HTTP/1.1",
":host", "square.com",
":scheme", "http",
"cache-control", "no-cache, no-store",
"set-cookie", "Cookie1\u0000Cookie2",
":status", "200 OK");
assertEquals(expected, headerBlock);
}
示例12: doSsl
import com.squareup.okhttp.Protocol; //导入依赖的package包/类
private Socket doSsl(Socket socket) throws IOException {
SSLSocket sslSocket =
(SSLSocket) sslSocketFactory.createSocket(socket, socket.getInetAddress().getHostAddress(),
socket.getPort(), true);
sslSocket.setUseClientMode(false);
NextProtoNego.put(sslSocket, new NextProtoNego.ServerProvider() {
@Override public void unsupported() {
System.out.println("UNSUPPORTED");
}
@Override public List<String> protocols() {
return Arrays.asList(Protocol.SPDY_3.name.utf8());
}
@Override public void protocolSelected(String protocol) {
System.out.println("PROTOCOL SELECTED: " + protocol);
}
});
return sslSocket;
}
示例13: pushPromises
import com.squareup.okhttp.Protocol; //导入依赖的package包/类
private void pushPromises(SpdyStream stream, List<PushPromise> promises) throws IOException {
for (PushPromise pushPromise : promises) {
List<Header> pushedHeaders = new ArrayList<Header>();
pushedHeaders.add(new Header(stream.getConnection().getProtocol() == Protocol.SPDY_3
? Header.TARGET_HOST
: Header.TARGET_AUTHORITY, getUrl(pushPromise.getPath()).getHost()));
pushedHeaders.add(new Header(Header.TARGET_METHOD, pushPromise.getMethod()));
pushedHeaders.add(new Header(Header.TARGET_PATH, pushPromise.getPath()));
for (int i = 0, size = pushPromise.getHeaders().size(); i < size; i++) {
String header = pushPromise.getHeaders().get(i);
String[] headerParts = header.split(":", 2);
if (headerParts.length != 2) {
throw new AssertionError("Unexpected header: " + header);
}
pushedHeaders.add(new Header(headerParts[0], headerParts[1].trim()));
}
String requestLine = pushPromise.getMethod() + ' ' + pushPromise.getPath() + " HTTP/1.1";
List<Integer> chunkSizes = Collections.emptyList(); // No chunked encoding for SPDY.
requestQueue.add(new RecordedRequest(requestLine, pushPromise.getHeaders(), chunkSizes, 0,
Util.EMPTY_BYTE_ARRAY, sequenceNumber.getAndIncrement(), socket));
byte[] pushedBody = pushPromise.getResponse().getBody();
SpdyStream pushedStream =
stream.getConnection().pushStream(stream.getId(), pushedHeaders, pushedBody.length > 0);
writeResponse(pushedStream, pushPromise.getResponse());
}
}
示例14: TwitterClient
import com.squareup.okhttp.Protocol; //导入依赖的package包/类
@Inject
public TwitterClient() {
if(restAdapter == null || iTwitterClient == null) {
OkHttpClient client = new OkHttpClient();
client.setProtocols(Arrays.asList(Protocol.HTTP_1_1, Protocol.HTTP_2, Protocol.SPDY_3));
restAdapter = new RestAdapter.Builder()
.setLogLevel(RestAdapter.LogLevel.FULL)
.setEndpoint(API_URL)
.setClient(new OkClient(client))
.build();
iTwitterClient = restAdapter.create(ITwitterClient.class);
}
}
示例15: createHttpClient
import com.squareup.okhttp.Protocol; //导入依赖的package包/类
/**
* @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;
}