下面列出了java.net.URLConnection#getRequestProperty ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void part2() throws Exception {
URL url = null;
String[] goodKeys = {"", "$", "key", "Key", " ", " "};
String[] goodValues = {"", "$", "value", "Value", " ", " "};
URLConnection conn = getConnection(url);
for (int i = 0; i < goodKeys.length; ++i) {
for (int j = 0; j < goodValues.length; ++j) {
// If a property with the key already exists, overwrite its value with the new value
conn.setRequestProperty(goodKeys[i], goodValues[j]);
String value = conn.getRequestProperty(goodKeys[i]);
if (!((goodValues[j] == null && value == null) || (value != null && value.equals(goodValues[j]))))
throw new RuntimeException("Method setRequestProperty(String,String) works incorrectly");
}
}
}
public static void part2() throws Exception {
URL url = null;
String[] goodKeys = {"", "$", "key", "Key", " ", " "};
String[] goodValues = {"", "$", "value", "Value", " ", " "};
URLConnection conn = getConnection(url);
for (int i = 0; i < goodKeys.length; ++i) {
for (int j = 0; j < goodValues.length; ++j) {
// If a property with the key already exists, overwrite its value with the new value
conn.setRequestProperty(goodKeys[i], goodValues[j]);
String value = conn.getRequestProperty(goodKeys[i]);
if (!((goodValues[j] == null && value == null) || (value != null && value.equals(goodValues[j]))))
throw new RuntimeException("Method setRequestProperty(String,String) works incorrectly");
}
}
}
public static void part2() throws Exception {
URL url = null;
String[] goodKeys = {"", "$", "key", "Key", " ", " "};
String[] goodValues = {"", "$", "value", "Value", " ", " "};
URLConnection conn = getConnection(url);
for (int i = 0; i < goodKeys.length; ++i) {
for (int j = 0; j < goodValues.length; ++j) {
// If a property with the key already exists, overwrite its value with the new value
conn.setRequestProperty(goodKeys[i], goodValues[j]);
String value = conn.getRequestProperty(goodKeys[i]);
if (!((goodValues[j] == null && value == null) || (value != null && value.equals(goodValues[j]))))
throw new RuntimeException("Method setRequestProperty(String,String) works incorrectly");
}
}
}
public static void part2() throws Exception {
URL url = null;
String[] goodKeys = {"", "$", "key", "Key", " ", " "};
String[] goodValues = {"", "$", "value", "Value", " ", " "};
URLConnection conn = getConnection(url);
for (int i = 0; i < goodKeys.length; ++i) {
for (int j = 0; j < goodValues.length; ++j) {
// If a property with the key already exists, overwrite its value with the new value
conn.setRequestProperty(goodKeys[i], goodValues[j]);
String value = conn.getRequestProperty(goodKeys[i]);
if (!((goodValues[j] == null && value == null) || (value != null && value.equals(goodValues[j]))))
throw new RuntimeException("Method setRequestProperty(String,String) works incorrectly");
}
}
}
public static void part2() throws Exception {
URL url = null;
String[] goodKeys = {"", "$", "key", "Key", " ", " "};
String[] goodValues = {"", "$", "value", "Value", " ", " "};
URLConnection conn = getConnection(url);
for (int i = 0; i < goodKeys.length; ++i) {
for (int j = 0; j < goodValues.length; ++j) {
// If a property with the key already exists, overwrite its value with the new value
conn.setRequestProperty(goodKeys[i], goodValues[j]);
String value = conn.getRequestProperty(goodKeys[i]);
if (!((goodValues[j] == null && value == null) || (value != null && value.equals(goodValues[j]))))
throw new RuntimeException("Method setRequestProperty(String,String) works incorrectly");
}
}
}
public static void part2() throws Exception {
URL url = null;
String[] goodKeys = {"", "$", "key", "Key", " ", " "};
String[] goodValues = {"", "$", "value", "Value", " ", " "};
URLConnection conn = getConnection(url);
for (int i = 0; i < goodKeys.length; ++i) {
for (int j = 0; j < goodValues.length; ++j) {
// If a property with the key already exists, overwrite its value with the new value
conn.setRequestProperty(goodKeys[i], goodValues[j]);
String value = conn.getRequestProperty(goodKeys[i]);
if (!((goodValues[j] == null && value == null) || (value != null && value.equals(goodValues[j]))))
throw new RuntimeException("Method setRequestProperty(String,String) works incorrectly");
}
}
}
public static void part2() throws Exception {
URL url = null;
String[] goodKeys = {"", "$", "key", "Key", " ", " "};
String[] goodValues = {"", "$", "value", "Value", " ", " "};
URLConnection conn = getConnection(url);
for (int i = 0; i < goodKeys.length; ++i) {
for (int j = 0; j < goodValues.length; ++j) {
// If a property with the key already exists, overwrite its value with the new value
conn.setRequestProperty(goodKeys[i], goodValues[j]);
String value = conn.getRequestProperty(goodKeys[i]);
if (!((goodValues[j] == null && value == null) || (value != null && value.equals(goodValues[j]))))
throw new RuntimeException("Method setRequestProperty(String,String) works incorrectly");
}
}
}
public static void part2() throws Exception {
URL url = null;
String[] goodKeys = {"", "$", "key", "Key", " ", " "};
String[] goodValues = {"", "$", "value", "Value", " ", " "};
URLConnection conn = getConnection(url);
for (int i = 0; i < goodKeys.length; ++i) {
for (int j = 0; j < goodValues.length; ++j) {
// If a property with the key already exists, overwrite its value with the new value
conn.setRequestProperty(goodKeys[i], goodValues[j]);
String value = conn.getRequestProperty(goodKeys[i]);
if (!((goodValues[j] == null && value == null) || (value != null && value.equals(goodValues[j]))))
throw new RuntimeException("Method setRequestProperty(String,String) works incorrectly");
}
}
}
public static void part2() throws Exception {
URL url = null;
String[] goodKeys = {"", "$", "key", "Key", " ", " "};
String[] goodValues = {"", "$", "value", "Value", " ", " "};
URLConnection conn = getConnection(url);
for (int i = 0; i < goodKeys.length; ++i) {
for (int j = 0; j < goodValues.length; ++j) {
// If a property with the key already exists, overwrite its value with the new value
conn.setRequestProperty(goodKeys[i], goodValues[j]);
String value = conn.getRequestProperty(goodKeys[i]);
if (!((goodValues[j] == null && value == null) || (value != null && value.equals(goodValues[j]))))
throw new RuntimeException("Method setRequestProperty(String,String) works incorrectly");
}
}
}
public static void part2() throws Exception {
URL url = null;
String[] goodKeys = {"", "$", "key", "Key", " ", " "};
String[] goodValues = {"", "$", "value", "Value", " ", " "};
URLConnection conn = getConnection(url);
for (int i = 0; i < goodKeys.length; ++i) {
for (int j = 0; j < goodValues.length; ++j) {
// If a property with the key already exists, overwrite its value with the new value
conn.setRequestProperty(goodKeys[i], goodValues[j]);
String value = conn.getRequestProperty(goodKeys[i]);
if (!((goodValues[j] == null && value == null) || (value != null && value.equals(goodValues[j]))))
throw new RuntimeException("Method setRequestProperty(String,String) works incorrectly");
}
}
}
public static void part2() throws Exception {
URL url = null;
String[] goodKeys = {"", "$", "key", "Key", " ", " "};
String[] goodValues = {"", "$", "value", "Value", " ", " "};
URLConnection conn = getConnection(url);
for (int i = 0; i < goodKeys.length; ++i) {
for (int j = 0; j < goodValues.length; ++j) {
// If a property with the key already exists, overwrite its value with the new value
conn.setRequestProperty(goodKeys[i], goodValues[j]);
String value = conn.getRequestProperty(goodKeys[i]);
if (!((goodValues[j] == null && value == null) || (value != null && value.equals(goodValues[j]))))
throw new RuntimeException("Method setRequestProperty(String,String) works incorrectly");
}
}
}
public static void part2() throws Exception {
URL url = null;
String[] goodKeys = {"", "$", "key", "Key", " ", " "};
String[] goodValues = {"", "$", "value", "Value", " ", " "};
URLConnection conn = getConnection(url);
for (int i = 0; i < goodKeys.length; ++i) {
for (int j = 0; j < goodValues.length; ++j) {
// If a property with the key already exists, overwrite its value with the new value
conn.setRequestProperty(goodKeys[i], goodValues[j]);
String value = conn.getRequestProperty(goodKeys[i]);
if (!((goodValues[j] == null && value == null) || (value != null && value.equals(goodValues[j]))))
throw new RuntimeException("Method setRequestProperty(String,String) works incorrectly");
}
}
}
public static void part2() throws Exception {
URL url = null;
String[] goodKeys = {"", "$", "key", "Key", " ", " "};
String[] goodValues = {"", "$", "value", "Value", " ", " "};
URLConnection conn = getConnection(url);
for (int i = 0; i < goodKeys.length; ++i) {
for (int j = 0; j < goodValues.length; ++j) {
// If a property with the key already exists, overwrite its value with the new value
conn.setRequestProperty(goodKeys[i], goodValues[j]);
String value = conn.getRequestProperty(goodKeys[i]);
if (!((goodValues[j] == null && value == null) || (value != null && value.equals(goodValues[j]))))
throw new RuntimeException("Method setRequestProperty(String,String) works incorrectly");
}
}
}
/**
* This procedure sets the URLConnection request properties
* from the PROTOCOL_HEADERS in the message.
*/
private void transferProtocolHeadersToURLConnection(URLConnection connection) {
boolean addHeaders = MessageUtils.getContextualBoolean(message, ADD_HEADERS_PROPERTY, false);
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
String header = entry.getKey();
if (HttpHeaderHelper.CONTENT_TYPE.equalsIgnoreCase(header)) {
continue;
}
List<String> headerList = entry.getValue();
if (addHeaders || HttpHeaderHelper.COOKIE.equalsIgnoreCase(header)) {
headerList.forEach(s -> connection.addRequestProperty(header, s));
} else {
connection.setRequestProperty(header, String.join(",", headerList));
}
}
// make sure we don't add more than one User-Agent header
if (connection.getRequestProperty("User-Agent") == null) {
connection.addRequestProperty("User-Agent", USER_AGENT);
}
}