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());
}
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();
}
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());
}
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]"));
}
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();
}
}
Aggregations