本文整理汇总了Java中java.net.HttpURLConnection.addRequestProperty方法的典型用法代码示例。如果您正苦于以下问题:Java HttpURLConnection.addRequestProperty方法的具体用法?Java HttpURLConnection.addRequestProperty怎么用?Java HttpURLConnection.addRequestProperty使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.HttpURLConnection
的用法示例。
在下文中一共展示了HttpURLConnection.addRequestProperty方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: varyMultipleFieldValuesWithMatch
import java.net.HttpURLConnection; //导入方法依赖的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();
HttpURLConnection multiConnection1 = openConnection(url);
multiConnection1.setRequestProperty("Accept-Language", "fr-CA, fr-FR");
multiConnection1.addRequestProperty("Accept-Language", "en-US");
assertEquals("A", readAscii(multiConnection1));
HttpURLConnection multiConnection2 = openConnection(url);
multiConnection2.setRequestProperty("Accept-Language", "fr-CA, fr-FR");
multiConnection2.addRequestProperty("Accept-Language", "en-US");
assertEquals("A", readAscii(multiConnection2));
}
示例2: rawWithAgent
import java.net.HttpURLConnection; //导入方法依赖的package包/类
private String rawWithAgent(String url) {
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestMethod("GET");
connection.setUseCaches(false);
connection.addRequestProperty("User-Agent", "Mozilla/4.76");
connection.setReadTimeout(15000);
connection.setConnectTimeout(15000);
connection.setDoOutput(true);
return IOUtils.toString(connection.getInputStream(), "UTF-8");
} catch (Exception e) {
JsonObject object = new JsonObject();
object.addProperty("success", false);
object.addProperty("cause", "Exception");
return object.toString();
}
}
示例3: partialRangeResponsesDoNotCorruptCache
import java.net.HttpURLConnection; //导入方法依赖的package包/类
@Test public void partialRangeResponsesDoNotCorruptCache() throws Exception {
// 1. request a range
// 2. request a full document, expecting a cache miss
server.enqueue(new MockResponse()
.setBody("AA")
.setResponseCode(HttpURLConnection.HTTP_PARTIAL)
.addHeader("Expires: " + formatDate(1, TimeUnit.HOURS))
.addHeader("Content-Range: bytes 1000-1001/2000"));
server.enqueue(new MockResponse()
.setBody("BB"));
URL url = server.url("/").url();
HttpURLConnection range = openConnection(url);
range.addRequestProperty("Range", "bytes=1000-1001");
assertEquals("AA", readAscii(range));
assertEquals("BB", readAscii(openConnection(url)));
}
示例4: isTokenInProxy
import java.net.HttpURLConnection; //导入方法依赖的package包/类
private boolean isTokenInProxy(String token) {
try {
URL authUrl = new URL(oAuthUrl);
HttpURLConnection connection = (HttpURLConnection) authUrl.openConnection();
connection.addRequestProperty("Cookie", "_oauth2_proxy=" + token);
connection.connect();
int resonseCode = connection.getResponseCode();
connection.disconnect();
logger.debug("Successfully checked token with oauth proxy, result {}", resonseCode);
return resonseCode == HttpStatus.ACCEPTED.value();
} catch (IOException e) {
logger.error("Failed to check session token at oauth Proxy");
return false;
}
}
示例5: varyMultipleFieldValuesWithNoMatch
import java.net.HttpURLConnection; //导入方法依赖的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();
HttpURLConnection multiConnection = openConnection(url);
multiConnection.setRequestProperty("Accept-Language", "fr-CA, fr-FR");
multiConnection.addRequestProperty("Accept-Language", "en-US");
assertEquals("A", readAscii(multiConnection));
HttpURLConnection notFrenchConnection = openConnection(url);
notFrenchConnection.setRequestProperty("Accept-Language", "fr-CA");
notFrenchConnection.addRequestProperty("Accept-Language", "en-US");
assertEquals("B", readAscii(notFrenchConnection));
}
示例6: assertClientSuppliedCondition
import java.net.HttpURLConnection; //导入方法依赖的package包/类
private RecordedRequest assertClientSuppliedCondition(MockResponse seed, String conditionName,
String conditionValue) throws Exception {
server.enqueue(seed.setBody("A"));
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
URL url = server.url("/").url();
assertEquals("A", readAscii(urlFactory.open(url)));
HttpURLConnection connection = urlFactory.open(url);
connection.addRequestProperty(conditionName, conditionValue);
assertEquals(HttpURLConnection.HTTP_NOT_MODIFIED, connection.getResponseCode());
assertEquals("", readAscii(connection));
server.takeRequest(); // seed
return server.takeRequest();
}
示例7: downloadIsSeperateBecauseGotoGotRemoved
import java.net.HttpURLConnection; //导入方法依赖的package包/类
/**
* God damn it Gosling, <a href="http://stackoverflow.com/a/4547764/3809164">reference here.</a>
*/
private void downloadIsSeperateBecauseGotoGotRemoved(File downloadTo) throws IOException {
URL url = new URL(downloadURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.addRequestProperty("User-Agent", AGENT);
connection.connect();
if (connection.getResponseCode() >= 300 && connection.getResponseCode() < 400) {
downloadURL = connection.getHeaderField("Location");
downloadIsSeperateBecauseGotoGotRemoved(downloadTo);
} else {
debug(connection.getResponseCode() + " " + connection.getResponseMessage() + " when requesting " + downloadURL);
copy(connection.getInputStream(), new FileOutputStream(downloadTo));
}
}
示例8: addBodyIfExists
import java.net.HttpURLConnection; //导入方法依赖的package包/类
private static void addBodyIfExists(HttpURLConnection connection, Request<?> request)
throws IOException, AuthFailureError {
byte[] body = request.getBody();
if (body != null) {
connection.setDoOutput(true);
connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getBodyContentType());
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.write(body);
out.close();
}
}
示例9: b
import java.net.HttpURLConnection; //导入方法依赖的package包/类
public static HttpURLConnection b(Context context, URL url) {
if (!"http".equals(url.getProtocol())) {
return (HttpURLConnection) url.openConnection();
}
if (c(context)) {
return (HttpURLConnection) url.openConnection(new Proxy(Type.HTTP, new
InetSocketAddress("10.0.0.200", 80)));
}
if (!b(context)) {
return (HttpURLConnection) url.openConnection();
}
HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(a(url)).openConnection();
httpURLConnection.addRequestProperty(Network.CMWAP_HEADER_HOST_KEY, url.getHost());
return httpURLConnection;
}
示例10: getResourceInputStream
import java.net.HttpURLConnection; //导入方法依赖的package包/类
@Override
protected InputStream getResourceInputStream(final Resource resource, final String entityId) throws IOException {
final String encodedId = URLEncoder.encode(entityId, "UTF-8");
final URL url = new URL(resource.getURL().toExternalForm().concat(encodedId));
final HttpURLConnection httpcon = (HttpURLConnection) (url.openConnection());
httpcon.setDoOutput(true);
httpcon.addRequestProperty("Accept", "*/*");
httpcon.setRequestMethod("GET");
httpcon.connect();
return httpcon.getInputStream();
}
开发者ID:hsj-xiaokang,项目名称:springboot-shiro-cas-mybatis,代码行数:13,代码来源:DynamicMetadataResolverAdapter.java
示例11: addHeader
import java.net.HttpURLConnection; //导入方法依赖的package包/类
private void addHeader(HttpURLConnection connection, Map<String, String> headers) {
if (headers == null || headers.size() == 0)
return;
for (Map.Entry<String, String> entry : headers.entrySet()) {
if (isCancelled.get())
return;
connection.addRequestProperty(entry.getKey(), entry.getValue());
}
}
示例12: clientSuppliedConditionWithoutCachedResult
import java.net.HttpURLConnection; //导入方法依赖的package包/类
@Test public void clientSuppliedConditionWithoutCachedResult() throws Exception {
server.enqueue(new MockResponse()
.setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
HttpURLConnection connection = openConnection(server.url("/").url());
String clientIfModifiedSince = formatDate(-24, TimeUnit.HOURS);
connection.addRequestProperty("If-Modified-Since", clientIfModifiedSince);
assertEquals(HttpURLConnection.HTTP_NOT_MODIFIED, connection.getResponseCode());
assertEquals("", readAscii(connection));
}
示例13: build
import java.net.HttpURLConnection; //导入方法依赖的package包/类
/**
* Builds the {@link HttpURLConnection} instance that can be used to execute the request.
*
* TODO: Remove @UsedForTesting after this method is actually used.
*/
@UsedForTesting
public HttpURLConnection build() throws IOException {
if (mUrl == null) {
throw new IllegalArgumentException("A URL must be specified!");
}
final HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection();
connection.setConnectTimeout(mConnectTimeoutMillis);
connection.setReadTimeout(mReadTimeoutMillis);
connection.setUseCaches(mUseCache);
switch (mMode) {
case MODE_UPLOAD_ONLY:
connection.setDoInput(true);
connection.setDoOutput(false);
break;
case MODE_DOWNLOAD_ONLY:
connection.setDoInput(false);
connection.setDoOutput(true);
break;
case MODE_BI_DIRECTIONAL:
connection.setDoInput(true);
connection.setDoOutput(true);
break;
}
for (final Entry<String, String> entry : mHeaderMap.entrySet()) {
connection.addRequestProperty(entry.getKey(), entry.getValue());
}
if (mContentLength >= 0) {
connection.setFixedLengthStreamingMode(mContentLength);
}
return connection;
}
示例14: setBody
import java.net.HttpURLConnection; //导入方法依赖的package包/类
private void setBody(HttpURLConnection con) throws IOException {
if (!formParams.isEmpty() && body != null) {
throw new RuntimeException("You can only specify body or form params, not both!");
}
String str = generateBody();
if (str != null) {
con.setDoOutput(true);
con.addRequestProperty("Content-Length", str.length() + "");
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(str);
wr.flush();
wr.close();
}
}
示例15: varyMatchesChangedRequestHeaderField
import java.net.HttpURLConnection; //导入方法依赖的package包/类
@Test public void varyMatchesChangedRequestHeaderField() 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();
HttpURLConnection frConnection = urlFactory.open(url);
frConnection.addRequestProperty("Accept-Language", "fr-CA");
assertEquals("A", readAscii(frConnection));
HttpURLConnection enConnection = urlFactory.open(url);
enConnection.addRequestProperty("Accept-Language", "en-US");
assertEquals("B", readAscii(enConnection));
}