当前位置: 首页>>代码示例>>Java>>正文


Java HttpURLConnection.setChunkedStreamingMode方法代码示例

本文整理汇总了Java中java.net.HttpURLConnection.setChunkedStreamingMode方法的典型用法代码示例。如果您正苦于以下问题:Java HttpURLConnection.setChunkedStreamingMode方法的具体用法?Java HttpURLConnection.setChunkedStreamingMode怎么用?Java HttpURLConnection.setChunkedStreamingMode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.net.HttpURLConnection的用法示例。


在下文中一共展示了HttpURLConnection.setChunkedStreamingMode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: streamedBodyIsNotRetried

import java.net.HttpURLConnection; //导入方法依赖的package包/类
@Test public void streamedBodyIsNotRetried() throws Exception {
  server.enqueue(new MockResponse()
      .setSocketPolicy(SocketPolicy.DISCONNECT_AFTER_REQUEST));

  urlFactory = new OkUrlFactory(defaultClient().newBuilder()
      .dns(new DoubleInetAddressDns())
      .build());
  HttpURLConnection connection = urlFactory.open(server.url("/").url());
  connection.setDoOutput(true);
  connection.setChunkedStreamingMode(100);
  OutputStream os = connection.getOutputStream();
  os.write("OutputStream is no fun.".getBytes("UTF-8"));
  os.close();

  try {
    connection.getResponseCode();
    fail();
  } catch (IOException expected) {
  }

  assertEquals(1, server.getRequestCount());
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:23,代码来源:URLConnectionTest.java

示例2: testPostOneMassiveWriteWriteOneByte

import java.net.HttpURLConnection; //导入方法依赖的package包/类
@SmallTest
@Feature({"Cronet"})
@CompareDefaultWithCronet
public void testPostOneMassiveWriteWriteOneByte() throws Exception {
    URL url = new URL(NativeTestServer.getEchoBodyURL());
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");
    connection.setChunkedStreamingMode(0);
    OutputStream out = connection.getOutputStream();
    byte[] largeData = TestUtil.getLargeData();
    for (int i = 0; i < largeData.length; i++) {
        out.write(largeData[i]);
    }
    assertEquals(200, connection.getResponseCode());
    assertEquals("OK", connection.getResponseMessage());
    TestUtil.checkLargeData(TestUtil.getResponseAsString(connection));
    connection.disconnect();
}
 
开发者ID:lizhangqu,项目名称:chromium-net-for-android,代码行数:20,代码来源:CronetChunkedOutputStreamTest.java

示例3: doUpload

import java.net.HttpURLConnection; //导入方法依赖的package包/类
private void doUpload(TransferKind uploadKind, WriteKind writeKind) throws Exception {
  int n = 512 * 1024;
  server.setBodyLimit(0);
  server.enqueue(new MockResponse());

  HttpURLConnection conn = urlFactory.open(server.url("/").url());
  conn.setDoOutput(true);
  conn.setRequestMethod("POST");
  if (uploadKind == TransferKind.CHUNKED) {
    conn.setChunkedStreamingMode(-1);
  } else {
    conn.setFixedLengthStreamingMode(n);
  }
  OutputStream out = conn.getOutputStream();
  if (writeKind == WriteKind.BYTE_BY_BYTE) {
    for (int i = 0; i < n; ++i) {
      out.write('x');
    }
  } else {
    byte[] buf = new byte[writeKind == WriteKind.SMALL_BUFFERS ? 256 : 64 * 1024];
    Arrays.fill(buf, (byte) 'x');
    for (int i = 0; i < n; i += buf.length) {
      out.write(buf, 0, Math.min(buf.length, n - i));
    }
  }
  out.close();
  assertEquals(200, conn.getResponseCode());
  RecordedRequest request = server.takeRequest();
  assertEquals(n, request.getBodySize());
  if (uploadKind == TransferKind.CHUNKED) {
    assertTrue(request.getChunkSizes().size() > 0);
  } else {
    assertTrue(request.getChunkSizes().isEmpty());
  }
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:36,代码来源:URLConnectionTest.java

示例4: testInitChunkedOutputStreamInConnect

import java.net.HttpURLConnection; //导入方法依赖的package包/类
@SmallTest
@Feature({"Cronet"})
@CompareDefaultWithCronet
public void testInitChunkedOutputStreamInConnect() throws Exception {
    URL url = new URL(NativeTestServer.getEchoBodyURL());
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);
    String dataString = "some very important chunked data";
    byte[] data = dataString.getBytes();
    connection.setChunkedStreamingMode(0);
    connection.connect();
    OutputStream out = connection.getOutputStream();
    out.write(data);
    assertEquals(200, connection.getResponseCode());
    assertEquals("OK", connection.getResponseMessage());
    assertEquals(dataString, TestUtil.getResponseAsString(connection));
    connection.disconnect();
}
 
开发者ID:lizhangqu,项目名称:chromium-net-for-android,代码行数:19,代码来源:CronetHttpURLConnectionTest.java

示例5: testWriteAfterRequestFailed

import java.net.HttpURLConnection; //导入方法依赖的package包/类
@SmallTest
@Feature({"Cronet"})
@CompareDefaultWithCronet
public void testWriteAfterRequestFailed() throws Exception {
    URL url = new URL(NativeTestServer.getEchoBodyURL());
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");
    connection.setChunkedStreamingMode(0);
    OutputStream out = connection.getOutputStream();
    out.write(UPLOAD_DATA);
    NativeTestServer.shutdownNativeTestServer();
    try {
        out.write(TestUtil.getLargeData());
        connection.getResponseCode();
        fail();
    } catch (IOException e) {
        if (!testingSystemHttpURLConnection()) {
            UrlRequestException requestException = (UrlRequestException) e;
            assertEquals(UrlRequestException.ERROR_CONNECTION_REFUSED,
                    requestException.getErrorCode());
        }
    }
    // Restarting server to run the test for a second time.
    assertTrue(NativeTestServer.startNativeTestServer(getContext()));
}
 
开发者ID:lizhangqu,项目名称:chromium-net-for-android,代码行数:27,代码来源:CronetChunkedOutputStreamTest.java

示例6: testPost

import java.net.HttpURLConnection; //导入方法依赖的package包/类
@SmallTest
@Feature({"Cronet"})
@CompareDefaultWithCronet
public void testPost() throws Exception {
    URL url = new URL(NativeTestServer.getEchoBodyURL());
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");
    connection.setChunkedStreamingMode(0);
    OutputStream out = connection.getOutputStream();
    out.write(UPLOAD_DATA);
    assertEquals(200, connection.getResponseCode());
    assertEquals("OK", connection.getResponseMessage());
    assertEquals(UPLOAD_DATA_STRING, TestUtil.getResponseAsString(connection));
    connection.disconnect();
}
 
开发者ID:lizhangqu,项目名称:chromium-net-for-android,代码行数:17,代码来源:CronetChunkedOutputStreamTest.java

示例7: testTransferEncodingHeaderSet

import java.net.HttpURLConnection; //导入方法依赖的package包/类
@SmallTest
@Feature({"Cronet"})
@CompareDefaultWithCronet
public void testTransferEncodingHeaderSet() throws Exception {
    URL url = new URL(NativeTestServer.getEchoHeaderURL("Transfer-Encoding"));
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");
    connection.setChunkedStreamingMode(0);
    OutputStream out = connection.getOutputStream();
    out.write(UPLOAD_DATA);
    assertEquals(200, connection.getResponseCode());
    assertEquals("OK", connection.getResponseMessage());
    assertEquals("chunked", TestUtil.getResponseAsString(connection));
    connection.disconnect();
}
 
开发者ID:lizhangqu,项目名称:chromium-net-for-android,代码行数:17,代码来源:CronetChunkedOutputStreamTest.java

示例8: testPostOneMassiveWrite

import java.net.HttpURLConnection; //导入方法依赖的package包/类
@SmallTest
@Feature({"Cronet"})
@CompareDefaultWithCronet
public void testPostOneMassiveWrite() throws Exception {
    URL url = new URL(NativeTestServer.getEchoBodyURL());
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");
    connection.setChunkedStreamingMode(0);
    OutputStream out = connection.getOutputStream();
    byte[] largeData = TestUtil.getLargeData();
    out.write(largeData);
    assertEquals(200, connection.getResponseCode());
    assertEquals("OK", connection.getResponseMessage());
    TestUtil.checkLargeData(TestUtil.getResponseAsString(connection));
    connection.disconnect();
}
 
开发者ID:lizhangqu,项目名称:chromium-net-for-android,代码行数:18,代码来源:CronetChunkedOutputStreamTest.java

示例9: testPostWriteOneByte

import java.net.HttpURLConnection; //导入方法依赖的package包/类
@SmallTest
@Feature({"Cronet"})
@CompareDefaultWithCronet
public void testPostWriteOneByte() throws Exception {
    URL url = new URL(NativeTestServer.getEchoBodyURL());
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");
    connection.setChunkedStreamingMode(0);
    OutputStream out = connection.getOutputStream();
    for (int i = 0; i < UPLOAD_DATA.length; i++) {
        out.write(UPLOAD_DATA[i]);
    }
    assertEquals(200, connection.getResponseCode());
    assertEquals("OK", connection.getResponseMessage());
    assertEquals(UPLOAD_DATA_STRING, TestUtil.getResponseAsString(connection));
    connection.disconnect();
}
 
开发者ID:lizhangqu,项目名称:chromium-net-for-android,代码行数:19,代码来源:CronetChunkedOutputStreamTest.java

示例10: streamedBodyWithClientRequestTimeout

import java.net.HttpURLConnection; //导入方法依赖的package包/类
@Test public void streamedBodyWithClientRequestTimeout() throws Exception {
  enqueueClientRequestTimeoutResponses();

  HttpURLConnection connection = urlFactory.open(server.url("/").url());
  connection.setRequestMethod("POST");
  connection.setChunkedStreamingMode(0);
  connection.getOutputStream().write("Hello".getBytes("UTF-8"));

  assertEquals(408, connection.getResponseCode());
  assertEquals(1, server.getRequestCount());
  connection.getErrorStream().close();
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:13,代码来源:URLConnectionTest.java

示例11: writeRequestFile

import java.net.HttpURLConnection; //导入方法依赖的package包/类
private void writeRequestFile(HttpURLConnection connection, String path)
    throws IOException {
  // Use MediaUtil.openMedia to open the file. This means that path could be file on the SD card,
  // an asset, a contact picture, etc.
  BufferedInputStream in = new BufferedInputStream(MediaUtil.openMedia(form, path));
  try {
    // Write the file's data.
    // According to the documentation at
    // http://developer.android.com/reference/java/net/HttpURLConnection.html
    // HttpURLConnection uses the GET method by default. It will use POST if setDoOutput(true) has
    // been called.
    connection.setDoOutput(true); // This makes it something other than a HTTP GET.
    connection.setChunkedStreamingMode(0);
    BufferedOutputStream out = new BufferedOutputStream(connection.getOutputStream());
    try {
      while (true) {
        int b = in.read();
        if (b == -1) {
          break;
        }
        out.write(b);
      }
      out.flush();
    } finally {
      out.close();
    }
  } finally {
    in.close();
  }
}
 
开发者ID:mit-cml,项目名称:appinventor-extensions,代码行数:31,代码来源:Web.java

示例12: testBug49424WithChunking

import java.net.HttpURLConnection; //导入方法依赖的package包/类
@Test
public void testBug49424WithChunking() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context root = tomcat.addContext("", null);
    Tomcat.addServlet(root, "Bug37794", new Bug37794Servlet());
    root.addServletMapping("/", "Bug37794");
    tomcat.start();

    HttpURLConnection conn = getConnection("http://localhost:" + getPort() + "/");
    conn.setChunkedStreamingMode(8 * 1024);
    InputStream is = conn.getInputStream();
    assertNotNull(is);
}
 
开发者ID:liaokailin,项目名称:tomcat7,代码行数:15,代码来源:TestRequest.java

示例13: test

import java.net.HttpURLConnection; //导入方法依赖的package包/类
void test(String method) throws Exception {
    ss = new ServerSocket(0);
    ss.setSoTimeout(ACCEPT_TIMEOUT);
    int port = ss.getLocalPort();

    Thread otherThread = new Thread(this);
    otherThread.start();

    try {
        URL url = new URL("http://localhost:" + port + "/");
        HttpURLConnection uc = (HttpURLConnection) url.openConnection();
        uc.setDoOutput(true);
        if (method != null)
            uc.setRequestMethod(method);
        uc.setChunkedStreamingMode(4096);
        OutputStream os = uc.getOutputStream();
        os.write("Hello there".getBytes());

        InputStream is = uc.getInputStream();
        is.close();
    } catch (IOException expected) {
        //expected.printStackTrace();
    } finally {
        ss.close();
        otherThread.join();
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:28,代码来源:StreamingRetry.java

示例14: connect

import java.net.HttpURLConnection; //导入方法依赖的package包/类
private HttpURLConnection connect() throws MalformedURLException, IOException {
  HttpURLConnection connection = (HttpURLConnection) buildRequestURL().openConnection();
  connection.setDoInput(true);
  connection.setDoOutput(true); 
  connection.setRequestMethod("POST");
  connection.setRequestProperty("Content-type", "audio/wav; codec=\"audio/pcm\"; samplerate=16000");
  connection.setRequestProperty("Accept", "application/json;text/xml");
  connection.setRequestProperty("Authorization", "Bearer " + auth.getToken());
  connection.setChunkedStreamingMode(0); // 0 == default chunk size
  connection.connect();

  return connection;
}
 
开发者ID:Azure-Samples,项目名称:SpeechToText-REST,代码行数:14,代码来源:SpeechClientREST.java

示例15: main

import java.net.HttpURLConnection; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {

        HTTPServer server = new HTTPServer();
        server.start();
        int port = server.getPort();
        out.println("Server listening on " + port);


        URL url = new URL("http://localhost:" + port);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setChunkedStreamingMode(1024);

        out.println("sending " + TOTAL_BYTES + " bytes");

        int byteAtOnce;
        int sendingBytes = TOTAL_BYTES;
        byte[] buffer = getBuffer(BUFFER_SIZE);
        try (OutputStream toServer = conn.getOutputStream()) {
            while (sendingBytes > 0) {
                if (sendingBytes > BUFFER_SIZE) {
                    byteAtOnce = BUFFER_SIZE;
                } else {
                    byteAtOnce = sendingBytes;
                }
                toServer.write(buffer, 0, byteAtOnce);
                sendingBytes -= byteAtOnce;
                out.print((TOTAL_BYTES - sendingBytes) + " was sent. ");
                toServer.flush();
                // gives the server thread time to read, and eventually close;
                Thread.sleep(500);
            }
        } catch (IOException expected) {
            // Expected IOException due to server.close()
            out.println("PASSED. Caught expected: " + expected);
            return;
        }

        // Expected IOException not received. FAIL
        throw new RuntimeException("Failed: Expected IOException not received");
    }
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:43,代码来源:CheckError.java


注:本文中的java.net.HttpURLConnection.setChunkedStreamingMode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。