本文整理汇总了Java中java.net.URLConnection.getRequestProperty方法的典型用法代码示例。如果您正苦于以下问题:Java URLConnection.getRequestProperty方法的具体用法?Java URLConnection.getRequestProperty怎么用?Java URLConnection.getRequestProperty使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.URLConnection
的用法示例。
在下文中一共展示了URLConnection.getRequestProperty方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: part2
import java.net.URLConnection; //导入方法依赖的package包/类
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");
}
}
}
示例2: validateProxy
import java.net.URLConnection; //导入方法依赖的package包/类
/**
* Validate a proxy over a given http page
* @param proxyAddress
* @param proxyPort
* @param testUrl the page to fetch (must include the third slash if just the host, like "http://somehost.com/"
* @param timeoutMillis
* @param username proxy basic authentication, or null when not needed
* @param password proxy basic authentication, or null when not needed
* @param userAgent to use when connecting (can be null for the default). E.g. "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0"
* @return the milliseconds taken to fetch the page, or -1 in case of error/timeout
*/
public long validateProxy(String proxyAddress, int proxyPort, URL testUrl, int timeoutMillis, String username, String password, String userAgent) {
long start = System.currentTimeMillis();
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyAddress, proxyPort));
try {
URLConnection connection = testUrl.openConnection(proxy);
connection.setReadTimeout(timeoutMillis);
connection.setConnectTimeout(timeoutMillis);
connection.setUseCaches(false);
connection.getRequestProperty(password);
if (userAgent!=null) {
connection.setRequestProperty("User-Agent", userAgent);
}
if (username!=null && password !=null) {
String auth = "Basic " + Base64Utils.encodeToString((username + ":" + password).getBytes());
connection.setRequestProperty("Proxy-Connection", "Keep-Alive");
connection.setRequestProperty("Proxy-Authorization", auth);
}
connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
connection.setRequestProperty("Accept-Encoding", "gzip, deflate");
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine = null;
while ((inputLine = in.readLine()) != null);
in.close();
return System.currentTimeMillis()-start;
} catch (IOException e) {
log.debug("Failed to validate proxy {}", proxyAddress, e);
}
return -1;
// Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.10.100.100", 80));
// HttpURLConnection connection =(HttpURLConnection)new URL("http://abc.abcd.com").openConnection(proxy);
// connection.setDoOutput(true);
// connection.setDoInput(true);
// connection.setRequestProperty("Content-type", "text/xml");
// connection.setRequestProperty("Accept", "text/xml, application/xml");
// connection.setRequestMethod("POST");
}