本文整理汇总了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();
}