Search in sources :

Example 41 with HttpURLConnection

use of java.net.HttpURLConnection in project jersey by jersey.

the class ContentTypeTest method testInvalidContentTypeHeader.

@Test
public void testInvalidContentTypeHeader() throws Exception {
    final URL url = new URL(getBaseUri().toString() + "ContentType");
    final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Accept", "text/plain");
    connection.setRequestProperty("Content-Type", "^^^");
    connection.setDoOutput(true);
    connection.connect();
    final OutputStream outputStream = connection.getOutputStream();
    outputStream.write("HelloWorld!".getBytes());
    outputStream.write('\n');
    outputStream.flush();
    assertEquals(400, connection.getResponseCode());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) OutputStream(java.io.OutputStream) URL(java.net.URL) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 42 with HttpURLConnection

use of java.net.HttpURLConnection in project jersey by jersey.

the class Jersey2794ITCase method mimeTempFileRemoved.

@Test
public void mimeTempFileRemoved() throws Exception {
    final String tempDir = System.getProperty("java.io.tmpdir");
    // Get number of matching MIME*tmp files (the number should be the same at the end of the test).
    final int expectedTempFiles = matchingTempFiles(tempDir);
    final URL url = new URL(getBaseUri().toString());
    final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("PUT");
    connection.setRequestProperty("Accept", "text/plain");
    connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=XXXX_YYYY");
    connection.setDoOutput(true);
    connection.connect();
    final OutputStream outputStream = connection.getOutputStream();
    outputStream.write("--XXXX_YYYY".getBytes());
    outputStream.write('\n');
    outputStream.write("Content-Type: text/plain".getBytes());
    outputStream.write('\n');
    outputStream.write("Content-Disposition: form-data; name=\"big-part\"".getBytes());
    outputStream.write('\n');
    outputStream.write('\n');
    // Send big chunk of data.
    for (int i = 0; i < 16 * 4096; i++) {
        outputStream.write('E');
        if (i % 1024 == 0) {
            outputStream.flush();
        }
    }
    // Do NOT send end of the MultiPart message to simulate the issue.
    // Get Response ...
    assertThat("Bad Request expected", connection.getResponseCode(), is(400));
    // Make sure that the Mimepull message and it's parts have been closed and temporary files deleted.
    assertThat("Temporary mimepull files were not deleted", matchingTempFiles(tempDir), is(expectedTempFiles));
    // ... Disconnect.
    connection.disconnect();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) OutputStream(java.io.OutputStream) URL(java.net.URL) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 43 with HttpURLConnection

use of java.net.HttpURLConnection in project jersey by jersey.

the class ApplicationHandlerITCase method testInvalidContentTypeHeader.

@Test
public void testInvalidContentTypeHeader() throws Exception {
    final URL url = new URL(getBaseUri().toString() + "ContentType");
    final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Accept", "text/plain");
    connection.setRequestProperty("Content-Type", "application/json, app/json");
    connection.setDoOutput(true);
    connection.connect();
    final OutputStream outputStream = connection.getOutputStream();
    outputStream.write("HelloWorld!".getBytes());
    outputStream.write('\n');
    outputStream.flush();
    assertEquals(400, connection.getResponseCode());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) OutputStream(java.io.OutputStream) URL(java.net.URL) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 44 with HttpURLConnection

use of java.net.HttpURLConnection in project jersey by jersey.

the class Jersey2164ITCase method testHeaderListMultipleHeaders.

/**
     * Check that multi value http headers are correctly read by the server.
     */
@Test
public void testHeaderListMultipleHeaders() throws Exception {
    final URL url = new URL(getBaseUri().toString());
    final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.setRequestProperty("Accept", "text/plain");
    connection.setRequestProperty("hello", "world");
    connection.addRequestProperty("hello", "universe");
    connection.setDoOutput(false);
    connection.connect();
    assertThat(connection.getResponseCode(), equalTo(200));
    assertThat(ReaderWriter.readFromAsString(new InputStreamReader(connection.getInputStream())), equalTo("2:[world, universe]"));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) URL(java.net.URL) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 45 with HttpURLConnection

use of java.net.HttpURLConnection in project ZhihuDailyPurify by izzyleung.

the class Http method get.

public static String get(String address) throws IOException {
    URL url = new URL(address);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setRequestMethod("GET");
    con.setRequestProperty("User-Agent", "Mozilla/5.0");
    try {
        if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuilder response = new StringBuilder();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            return response.toString();
        } else {
            throw new IOException("Network Error - response code: " + con.getResponseCode());
        }
    } finally {
        con.disconnect();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

HttpURLConnection (java.net.HttpURLConnection)3831 URL (java.net.URL)2447 IOException (java.io.IOException)1634 InputStream (java.io.InputStream)1082 InputStreamReader (java.io.InputStreamReader)692 Test (org.junit.Test)650 BufferedReader (java.io.BufferedReader)573 OutputStream (java.io.OutputStream)466 MalformedURLException (java.net.MalformedURLException)372 URLConnection (java.net.URLConnection)248 HashMap (java.util.HashMap)216 OutputStreamWriter (java.io.OutputStreamWriter)208 Map (java.util.Map)199 Gson (com.google.gson.Gson)190 ByteArrayOutputStream (java.io.ByteArrayOutputStream)186 ArrayList (java.util.ArrayList)168 ExecutionException (java.util.concurrent.ExecutionException)161 File (java.io.File)159 AsyncTask (android.os.AsyncTask)158 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)157