本文整理匯總了Java中java.net.HttpURLConnection.getRequestProperties方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpURLConnection.getRequestProperties方法的具體用法?Java HttpURLConnection.getRequestProperties怎麽用?Java HttpURLConnection.getRequestProperties使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.net.HttpURLConnection
的用法示例。
在下文中一共展示了HttpURLConnection.getRequestProperties方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testAddAndSetRequestPropertyWithSameKey
import java.net.HttpURLConnection; //導入方法依賴的package包/類
@SmallTest
@Feature({"Cronet"})
@CompareDefaultWithCronet
public void testAddAndSetRequestPropertyWithSameKey() throws Exception {
URL url = new URL(NativeTestServer.getEchoAllHeadersURL());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.addRequestProperty("header-name", "value1");
connection.setRequestProperty("Header-nAme", "value2");
// Before connection is made, check request headers are set.
assertEquals("value2", connection.getRequestProperty("header-namE"));
Map<String, List<String>> requestHeadersMap =
connection.getRequestProperties();
assertEquals(1, requestHeadersMap.get("HeAder-name").size());
assertEquals("value2", requestHeadersMap.get("HeAder-name").get(0));
// Check the request headers echoed back by the server.
assertEquals(200, connection.getResponseCode());
assertEquals("OK", connection.getResponseMessage());
String headers = TestUtil.getResponseAsString(connection);
List<String> actualValues =
getRequestHeaderValues(headers, "Header-nAme");
assertEquals(1, actualValues.size());
assertEquals("value2", actualValues.get(0));
connection.disconnect();
}
示例2: testAddRequestProperty
import java.net.HttpURLConnection; //導入方法依賴的package包/類
@SmallTest
@Feature({"Cronet"})
@CompareDefaultWithCronet
public void testAddRequestProperty() throws Exception {
URL url = new URL(NativeTestServer.getEchoAllHeadersURL());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.addRequestProperty("foo-header", "foo");
connection.addRequestProperty("bar-header", "bar");
// Before connection is made, check request headers are set.
Map<String, List<String>> requestHeadersMap =
connection.getRequestProperties();
List<String> fooValues = requestHeadersMap.get("foo-header");
assertEquals(1, fooValues.size());
assertEquals("foo", fooValues.get(0));
assertEquals("foo", connection.getRequestProperty("foo-header"));
List<String> barValues = requestHeadersMap.get("bar-header");
assertEquals(1, barValues.size());
assertEquals("bar", barValues.get(0));
assertEquals("bar", connection.getRequestProperty("bar-header"));
// Check the request headers echoed back by the server.
assertEquals(200, connection.getResponseCode());
assertEquals("OK", connection.getResponseMessage());
String headers = TestUtil.getResponseAsString(connection);
List<String> fooHeaderValues =
getRequestHeaderValues(headers, "foo-header");
List<String> barHeaderValues =
getRequestHeaderValues(headers, "bar-header");
assertEquals(1, fooHeaderValues.size());
assertEquals("foo", fooHeaderValues.get(0));
assertEquals(1, fooHeaderValues.size());
assertEquals("bar", barHeaderValues.get(0));
connection.disconnect();
}
示例3: testSetRequestPropertyWithSameKey
import java.net.HttpURLConnection; //導入方法依賴的package包/類
@SmallTest
@Feature({"Cronet"})
@CompareDefaultWithCronet
public void testSetRequestPropertyWithSameKey() throws Exception {
URL url = new URL(NativeTestServer.getEchoAllHeadersURL());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// The test always sets and retrieves one header with the same
// capitalization, and the other header with slightly different
// capitalization.
conn.setRequestProperty("same-capitalization", "yo");
conn.setRequestProperty("diFFerent-cApitalization", "foo");
Map<String, List<String>> headersMap = conn.getRequestProperties();
List<String> values1 = headersMap.get("same-capitalization");
assertEquals(1, values1.size());
assertEquals("yo", values1.get(0));
assertEquals("yo", conn.getRequestProperty("same-capitalization"));
List<String> values2 = headersMap.get("different-capitalization");
assertEquals(1, values2.size());
assertEquals("foo", values2.get(0));
assertEquals("foo",
conn.getRequestProperty("Different-capitalization"));
// Check request header is updated.
conn.setRequestProperty("same-capitalization", "hi");
conn.setRequestProperty("different-Capitalization", "bar");
Map<String, List<String>> newHeadersMap = conn.getRequestProperties();
List<String> newValues1 = newHeadersMap.get("same-capitalization");
assertEquals(1, newValues1.size());
assertEquals("hi", newValues1.get(0));
assertEquals("hi", conn.getRequestProperty("same-capitalization"));
List<String> newValues2 = newHeadersMap.get("differENT-capitalization");
assertEquals(1, newValues2.size());
assertEquals("bar", newValues2.get(0));
assertEquals("bar",
conn.getRequestProperty("different-capitalization"));
// Check the request headers echoed back by the server.
assertEquals(200, conn.getResponseCode());
assertEquals("OK", conn.getResponseMessage());
String headers = TestUtil.getResponseAsString(conn);
List<String> actualValues1 =
getRequestHeaderValues(headers, "same-capitalization");
assertEquals(1, actualValues1.size());
assertEquals("hi", actualValues1.get(0));
List<String> actualValues2 =
getRequestHeaderValues(headers, "different-Capitalization");
assertEquals(1, actualValues2.size());
assertEquals("bar", actualValues2.get(0));
conn.disconnect();
}