本文整理汇总了Java中java.net.URLConnection.addRequestProperty方法的典型用法代码示例。如果您正苦于以下问题:Java URLConnection.addRequestProperty方法的具体用法?Java URLConnection.addRequestProperty怎么用?Java URLConnection.addRequestProperty使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.URLConnection
的用法示例。
在下文中一共展示了URLConnection.addRequestProperty方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: varyMultipleFieldsWithNoMatch
import java.net.URLConnection; //导入方法依赖的package包/类
@Test public void varyMultipleFieldsWithNoMatch() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60")
.addHeader("Vary: Accept-Language, Accept-Charset")
.addHeader("Vary: Accept-Encoding")
.setBody("A"));
server.enqueue(new MockResponse().setBody("B"));
URL url = server.url("/").url();
URLConnection frConnection = urlFactory.open(url);
frConnection.addRequestProperty("Accept-Language", "fr-CA");
frConnection.addRequestProperty("Accept-Charset", "UTF-8");
frConnection.addRequestProperty("Accept-Encoding", "identity");
assertEquals("A", readAscii(frConnection));
URLConnection enConnection = urlFactory.open(url);
enConnection.addRequestProperty("Accept-Language", "en-CA");
enConnection.addRequestProperty("Accept-Charset", "UTF-8");
enConnection.addRequestProperty("Accept-Encoding", "identity");
assertEquals("B", readAscii(enConnection));
}
示例2: doInBackground
import java.net.URLConnection; //导入方法依赖的package包/类
@Override
protected Bitmap doInBackground(String... strings) {
String cookie = strings[0];
String url = "https://spps.getalma.com/";
Bitmap temp = null;
try {
Document document = Jsoup.connect(url + "home").timeout(0).header("Cookie", cookie).get();
String imageUrl = document.select("ul > " +
"li.pure-menu-item.pure-menu-has-children.pure-menu-allow-hover.user > a > img").attr("abs:data-src");
Log.d("ImageUrl", imageUrl);
Log.d("ImageUrlCookie", cookie);
URLConnection connection = new java.net.URL(imageUrl).openConnection();
connection.addRequestProperty("Cookie", cookie);
connection.connect();
temp = BitmapFactory.decodeStream(connection.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
return temp;
}
示例3: responseCacheRequestHeaders
import java.net.URLConnection; //导入方法依赖的package包/类
@Test public void responseCacheRequestHeaders() throws IOException, URISyntaxException {
server.enqueue(new MockResponse()
.setBody("ABC"));
final AtomicReference<Map<String, List<String>>> requestHeadersRef = new AtomicReference<>();
setInternalCache(new CacheAdapter(new AbstractResponseCache() {
@Override public CacheResponse get(URI uri, String requestMethod,
Map<String, List<String>> requestHeaders) throws IOException {
requestHeadersRef.set(requestHeaders);
return null;
}
}));
URL url = server.url("/").url();
URLConnection urlConnection = openConnection(url);
urlConnection.addRequestProperty("A", "android");
readAscii(urlConnection);
assertEquals(Arrays.asList("android"), requestHeadersRef.get().get("A"));
}
示例4: varyMultipleFieldValuesWithNoMatch
import java.net.URLConnection; //导入方法依赖的package包/类
@Test public void varyMultipleFieldValuesWithNoMatch() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60")
.addHeader("Vary: Accept-Language")
.setBody("A"));
server.enqueue(new MockResponse().setBody("B"));
URL url = server.url("/").url();
URLConnection connection1 = urlFactory.open(url);
connection1.addRequestProperty("Accept-Language", "fr-CA, fr-FR");
connection1.addRequestProperty("Accept-Language", "en-US");
assertEquals("A", readAscii(connection1));
URLConnection connection2 = urlFactory.open(url);
connection2.addRequestProperty("Accept-Language", "fr-CA");
connection2.addRequestProperty("Accept-Language", "en-US");
assertEquals("B", readAscii(connection2));
}
示例5: varyMultipleFieldValuesWithMatch
import java.net.URLConnection; //导入方法依赖的package包/类
@Test public void varyMultipleFieldValuesWithMatch() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60")
.addHeader("Vary: Accept-Language")
.setBody("A"));
server.enqueue(new MockResponse().setBody("B"));
URL url = server.url("/").url();
URLConnection connection1 = urlFactory.open(url);
connection1.addRequestProperty("Accept-Language", "fr-CA, fr-FR");
connection1.addRequestProperty("Accept-Language", "en-US");
assertEquals("A", readAscii(connection1));
URLConnection connection2 = urlFactory.open(url);
connection2.addRequestProperty("Accept-Language", "fr-CA, fr-FR");
connection2.addRequestProperty("Accept-Language", "en-US");
assertEquals("A", readAscii(connection2));
}
示例6: isVersionBlacklisted
import java.net.URLConnection; //导入方法依赖的package包/类
public static boolean isVersionBlacklisted(int ver) throws IOException{
URL url = new URL(FETCH_URL + "disabled.txt");
URLConnection conn = url.openConnection();
conn.addRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36(KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36");
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String strLine;
while ((strLine = reader.readLine()) != null) {
int ver1 = 0;
try{
ver1 = Integer.parseInt(strLine);
}catch(Exception e){
return false;
}
if(ver <= ver1) return true;
}
reader.close();
conn.getInputStream().close();
return false;
}
示例7: varyFieldsAreCaseInsensitive
import java.net.URLConnection; //导入方法依赖的package包/类
@Test public void varyFieldsAreCaseInsensitive() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60")
.addHeader("Vary: ACCEPT-LANGUAGE")
.setBody("A"));
server.enqueue(new MockResponse().setBody("B"));
URL url = server.url("/").url();
URLConnection connection1 = urlFactory.open(url);
connection1.addRequestProperty("Accept-Language", "fr-CA");
assertEquals("A", readAscii(connection1));
URLConnection connection2 = urlFactory.open(url);
connection2.addRequestProperty("accept-language", "fr-CA");
assertEquals("A", readAscii(connection2));
}
示例8: requestMaxStaleNotHonoredWithMustRevalidate
import java.net.URLConnection; //导入方法依赖的package包/类
@Test public void requestMaxStaleNotHonoredWithMustRevalidate() throws IOException {
server.enqueue(new MockResponse()
.setBody("A")
.addHeader("Cache-Control: max-age=120, must-revalidate")
.addHeader("Date: " + formatDate(-4, TimeUnit.MINUTES)));
server.enqueue(new MockResponse()
.setBody("B"));
assertEquals("A", readAscii(openConnection(server.url("/").url())));
URLConnection connection = openConnection(server.url("/").url());
connection.addRequestProperty("Cache-Control", "max-stale=180");
assertEquals("B", readAscii(connection));
}
示例9: clientConfiguredCustomContentEncoding
import java.net.URLConnection; //导入方法依赖的package包/类
@Test public void clientConfiguredCustomContentEncoding() throws Exception {
server.enqueue(new MockResponse().setBody("ABCDE").addHeader("Content-Encoding: custom"));
URLConnection connection = urlFactory.open(server.url("/").url());
connection.addRequestProperty("Accept-Encoding", "custom");
assertEquals("ABCDE", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
RecordedRequest request = server.takeRequest();
assertEquals("custom", request.getHeader("Accept-Encoding"));
}
示例10: downloadZip
import java.net.URLConnection; //导入方法依赖的package包/类
/** Downloads a zip from the url, into a temp file under the given temp dir. */
@SuppressForbidden(reason = "We use getInputStream to download plugins")
private Path downloadZip(Terminal terminal, String urlString, Path tmpDir) throws IOException {
terminal.println(VERBOSE, "Retrieving zip from " + urlString);
URL url = new URL(urlString);
Path zip = Files.createTempFile(tmpDir, null, ".zip");
URLConnection urlConnection = url.openConnection();
urlConnection.addRequestProperty("User-Agent", "elasticsearch-plugin-installer");
int contentLength = urlConnection.getContentLength();
try (InputStream in = new TerminalProgressInputStream(urlConnection.getInputStream(), contentLength, terminal)) {
// must overwrite since creating the temp file above actually created the file
Files.copy(in, zip, StandardCopyOption.REPLACE_EXISTING);
}
return zip;
}
示例11: requestMaxAge
import java.net.URLConnection; //导入方法依赖的package包/类
@Test public void requestMaxAge() throws IOException {
server.enqueue(new MockResponse().setBody("A")
.addHeader("Last-Modified: " + formatDate(-2, TimeUnit.HOURS))
.addHeader("Date: " + formatDate(-1, TimeUnit.MINUTES))
.addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)));
server.enqueue(new MockResponse().setBody("B"));
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
URLConnection connection = urlFactory.open(server.url("/").url());
connection.addRequestProperty("Cache-Control", "max-age=30");
assertEquals("B", readAscii(connection));
}
示例12: setUp
import java.net.URLConnection; //导入方法依赖的package包/类
@Before
public void setUp() throws IOException {
// Chart is arbitrary, but it does have subcharts in it, which exercise some tricky logic
final URI uri = URI.create("https://kubernetes-charts.storage.googleapis.com/wordpress-0.6.6.tgz");
assertNotNull(uri);
final URL url = uri.toURL();
assertNotNull(url);
final URLConnection connection = url.openConnection();
assertNotNull(connection);
connection.addRequestProperty("Accept-Encoding", "gzip");
connection.connect();
assertEquals("application/x-tar", connection.getContentType());
this.stream = new TarInputStream(new BufferedInputStream(new GZIPInputStream(connection.getInputStream())));
}
示例13: responseSourceHeaderCached
import java.net.URLConnection; //导入方法依赖的package包/类
@Test public void responseSourceHeaderCached() throws IOException {
server.enqueue(new MockResponse().setBody("A")
.addHeader("Cache-Control: max-age=30")
.addHeader("Date: " + formatDate(0, TimeUnit.MINUTES)));
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
URLConnection connection = urlFactory.open(server.url("/").url());
connection.addRequestProperty("Cache-Control", "only-if-cached");
assertEquals("A", readAscii(connection));
}
示例14: authorizationRequestFullyCached
import java.net.URLConnection; //导入方法依赖的package包/类
@Test public void authorizationRequestFullyCached() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").setBody("A"));
server.enqueue(new MockResponse().setBody("B"));
URL url = server.url("/").url();
URLConnection connection = urlFactory.open(url);
connection.addRequestProperty("Authorization", "password");
assertEquals("A", readAscii(connection));
assertEquals("A", readAscii(urlFactory.open(url)));
}
示例15: requestMaxStale
import java.net.URLConnection; //导入方法依赖的package包/类
@Test public void requestMaxStale() throws IOException {
server.enqueue(new MockResponse().setBody("A")
.addHeader("Cache-Control: max-age=120")
.addHeader("Date: " + formatDate(-4, TimeUnit.MINUTES)));
server.enqueue(new MockResponse().setBody("B"));
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
URLConnection connection = urlFactory.open(server.url("/").url());
connection.addRequestProperty("Cache-Control", "max-stale=180");
assertEquals("A", readAscii(connection));
assertEquals("110 HttpURLConnection \"Response is stale\"",
connection.getHeaderField("Warning"));
}