本文整理了Java中java.net.HttpURLConnection.getHeaderField()
方法的一些代码示例,展示了HttpURLConnection.getHeaderField()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpURLConnection.getHeaderField()
方法的具体详情如下:
包路径:java.net.HttpURLConnection
类名称:HttpURLConnection
方法名:getHeaderField
[英]Returns the value for the n
th header field. Some implementations may treat the 0
th header field as special, i.e. as the status line returned by the HTTP server.
This method can be used in conjunction with the #getHeaderFieldKey method to iterate through all the headers in the message.
[中]返回第n
个标头字段的值。某些实现可能将第0
个头字段视为特殊字段,即HTTP服务器返回的状态行。
此方法可与#getHeaderFieldKey方法结合使用,以迭代消息中的所有头。
代码示例来源:origin: frohoff/ysoserial
public static InetSocketAddress getCliPort ( String jenkinsUrl ) throws MalformedURLException, IOException {
URL u = new URL(jenkinsUrl);
URLConnection conn = u.openConnection();
if ( ! ( conn instanceof HttpURLConnection ) ) {
System.err.println("Not a HTTP URL");
throw new MalformedURLException();
}
HttpURLConnection hc = (HttpURLConnection) conn;
if ( hc.getResponseCode() >= 400 ) {
System.err.println("* Error connection to jenkins HTTP " + u);
}
int clip = Integer.parseInt(hc.getHeaderField("X-Jenkins-CLI-Port"));
return new InetSocketAddress(u.getHost(), clip);
}
代码示例来源:origin: kiegroup/optaplanner
private void redirectConnection(HttpURLConnection http) throws IOException {
URL base = http.getURL();
String location = http.getHeaderField("Location");
URL target = null;
if (location != null) {
target = new URL(base, location);
}
http.disconnect();
// Redirection should be allowed only for HTTP and HTTPS
// and should be limited to 5 redirections at most.
if (target == null || !(target.getProtocol().equals("http")
|| target.getProtocol().equals("https"))
|| redirects >= 5) {
throw new SecurityException("illegal URL redirect");
}
isRedirect = true;
connection = target.openConnection();
redirects++;
}
}
代码示例来源:origin: robovm/robovm
getInputStream();
String response = getHeaderField(0);
if (response == null) {
return -1;
代码示例来源:origin: cucumber/cucumber-jvm
@Override
public void close() throws IOException {
try {
if (urlConnection != null) {
int responseCode = urlConnection.getResponseCode();
if (responseCode != expectedResponseCode) {
try {
urlConnection.getInputStream().close();
throw new IOException(String.format("Expected response code: %d. Got: %d", expectedResponseCode, responseCode));
} catch (IOException expected) {
InputStream errorStream = urlConnection.getErrorStream();
if (errorStream != null) {
String responseBody = FixJava.readReader(new InputStreamReader(errorStream, "UTF-8"));
String contentType = urlConnection.getHeaderField("Content-Type");
if (contentType == null) {
contentType = "text/plain";
}
throw new ResponseException(responseBody, expected, responseCode, contentType);
} else {
throw expected;
}
}
}
}
} finally {
out.close();
}
}
代码示例来源:origin: webex/spark-java-sdk
private void parseLinks(HttpURLConnection connection) throws IOException {
urls.clear();
String link = connection.getHeaderField("Link");
if (link != null && !"".equals(link)) {
Matcher matcher = linkPattern.matcher(link);
while (matcher.find()) {
String url = matcher.group(1);
String foundRel = matcher.group(2);
urls.put(foundRel, new URL(url));
}
}
}
代码示例来源:origin: google/ExoPlayer
URL url = new URL(dataSpec.uri.toString());
@HttpMethod int httpMethod = dataSpec.httpMethod;
byte[] httpBody = dataSpec.httpBody;
makeConnection(
url, httpMethod, httpBody, position, length, allowGzip, false /* followRedirects */);
int responseCode = connection.getResponseCode();
String location = connection.getHeaderField("Location");
if ((httpMethod == DataSpec.HTTP_METHOD_GET || httpMethod == DataSpec.HTTP_METHOD_HEAD)
&& (responseCode == HttpURLConnection.HTTP_MULT_CHOICE
代码示例来源:origin: org.apache.hadoop/hadoop-common
separator = "&";
url = new URL(sb.toString());
AuthenticatedURL aUrl = new AuthenticatedURL(this, connConfigurator);
org.apache.hadoop.security.token.Token<AbstractDelegationTokenIdentifier>
HttpExceptionUtils.validateResponse(conn, HttpURLConnection.HTTP_OK);
if (hasResponse) {
String contentType = conn.getHeaderField(CONTENT_TYPE);
contentType =
(contentType != null) ? StringUtils.toLowerCase(contentType) : null;
contentType.contains(APPLICATION_JSON_MIME)) {
try {
ret = JsonSerialization.mapReader().readValue(conn.getInputStream());
} catch (Exception ex) {
throw new AuthenticationException(String.format(
代码示例来源:origin: aa112901/remusic
HttpURLConnection response = HttpUtils.send(url.openConnection());
if (response == null) {
return false;
int contentLength = Integer.valueOf(response.getHeaderField(Constants.CONTENT_LENGTH));
cacheDao.insertOrUpdate(fileUtils.getFileName(), contentLength);
InputStream data = response.getInputStream();
byte[] buff = new byte[1024 * 40];
int readBytes = 0;
代码示例来源:origin: apache/oozie
@Override
public Void call() throws Exception {
MockDagEngineService.reset();
Map<String, String> params = new HashMap<String, String>();
params.put(RestConstants.JOB_SHOW_PARAM, RestConstants.JOB_SHOW_GRAPH);
URL url = createURL(MockDagEngineService.JOB_ID + 1 + MockDagEngineService.JOB_ID_END, params);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
assertEquals(HttpServletResponse.SC_BAD_REQUEST, conn.getResponseCode());
assertEquals(ErrorCode.E0306.name(), conn.getHeaderField(RestConstants.OOZIE_ERROR_CODE));
return null;
}
});
代码示例来源:origin: jenkinsci/jenkins
int responseCode = httpCon.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_MOVED_PERM
|| responseCode == HttpURLConnection.HTTP_MOVED_TEMP) {
String location = httpCon.getHeaderField("Location");
listener.getLogger().println("Following redirect " + archive.toExternalForm() + " -> " + location);
return installIfNecessaryFrom(getUrlFactory().newURL(location), listener, message, maxRedirects - 1);
代码示例来源:origin: pmd/pmd
private int computeHttpResponse(String url) {
try {
final HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(url).openConnection();
httpURLConnection.setRequestMethod("HEAD");
httpURLConnection.setConnectTimeout(5000);
httpURLConnection.setReadTimeout(15000);
httpURLConnection.connect();
final int responseCode = httpURLConnection.getResponseCode();
String response = "HTTP " + responseCode;
if (httpURLConnection.getHeaderField("Location") != null) {
response += ", Location: " + httpURLConnection.getHeaderField("Location");
}
LOG.fine("response: " + response + " on " + url);
// success (HTTP 2xx) or redirection (HTTP 3xx)
return responseCode;
} catch (IOException ex) {
LOG.fine("response: " + ex.getClass().getName() + " on " + url + " : " + ex.getMessage());
return 599;
}
}
代码示例来源:origin: yuliskov/SmartYouTubeTV
protected String doInBackground(String... urls)
{
URL url;
String filename = null;
try {
url = new URL(urls[0]);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
conn.setInstanceFollowRedirects(false);
String depo = conn.getHeaderField("Content-Disposition");
if (depo != null)
filename = depo.replaceFirst("(?i)^.*filename=\"?([^\"]+)\"?.*$", "$1");
else
filename = URLUtil.guessFileName(urls[0], null, null);
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
}
return filename;
}
代码示例来源:origin: nostra13/Android-Universal-Image-Loader
while (conn.getResponseCode() / 100 == 3 && redirectCount < MAX_REDIRECT_COUNT) {
conn = createConnection(conn.getHeaderField("Location"), extra);
redirectCount++;
imageStream = conn.getInputStream();
} catch (IOException e) {
throw new IOException("Image request failed with response code " + conn.getResponseCode());
代码示例来源:origin: com.cloudbees.mtslaves/mansion-client
public SnapshotRef snapshot() throws IOException {
HttpURLConnection con = postJson(open("snapshot"),new JSONObject());
verifyResponseStatus(con);
return new SnapshotRef(new URL(con.getHeaderField("Location")), token);
}
代码示例来源:origin: guolindev/giffun
return null;
final int statusCode = urlConnection.getResponseCode();
if (statusCode / 100 == 2) {
return getStreamForSuccessfulRequest(urlConnection);
} else if (statusCode / 100 == 3) {
String redirectUrlString = urlConnection.getHeaderField("Location");
if (TextUtils.isEmpty(redirectUrlString)) {
throw new IOException("Received empty or null redirect url");
URL redirectUrl = new URL(url, redirectUrlString);
return loadDataWithRedirects(redirectUrl, redirects + 1, url, headers);
} else {
代码示例来源:origin: DozerMapper/dozer
/**
* NOTE: GitHub.io returns 301 so we need to handle redirection here, else SAX fails.
*
* @param systemId systemId used by XSD
* @return stream to XSD
* @throws IOException if fails to find XSD
*/
private InputSource resolveFromURL(String systemId) throws IOException {
log.debug("Trying to download [{}]", systemId);
URL obj = new URL(systemId);
HttpURLConnection conn = (HttpURLConnection)obj.openConnection();
int status = conn.getResponseCode();
if ((status != HttpURLConnection.HTTP_OK)
&& (status == HttpURLConnection.HTTP_MOVED_TEMP
|| status == HttpURLConnection.HTTP_MOVED_PERM
|| status == HttpURLConnection.HTTP_SEE_OTHER)) {
log.debug("Received status of {}, attempting to follow Location header", status);
String newUrl = conn.getHeaderField("Location");
conn = (HttpURLConnection)new URL(newUrl).openConnection();
}
return new InputSource(conn.getInputStream());
}
}
代码示例来源:origin: ron190/jsql-injection
targetUrl = new URL(this.urlAdminPage);
} catch (MalformedURLException e) {
isUrlIncorrect = true;
HttpURLConnection connection = (HttpURLConnection) targetUrl.openConnection();
this.responseCodeHttp = ObjectUtils.firstNonNull(connection.getHeaderField(0), "");
代码示例来源:origin: spring-projects/spring-security-oauth
out.close();
responseCode = connection.getResponseCode();
responseMessage = connection.getResponseMessage();
if (responseMessage == null) {
return connection.getInputStream();
String authHeaderValue = connection.getHeaderField("WWW-Authenticate");
if (authHeaderValue != null) {
Map<String, String> headerEntries = StringSplitUtils.splitEachArrayElementAndCreateMap(StringSplitUtils.splitIgnoringQuotes(authHeaderValue, ','), "=", "\"");
代码示例来源:origin: com.cloudbees.mtslaves/mansion-client
/**
* @param _for
* Account for which the virtual machine is assigned to.
*/
public VirtualMachineRef createVirtualMachine(HardwareSpec spec, String _for) throws IOException {
JsonConfig jsc = new JsonConfig();
jsc.setIgnoreTransientFields(true);
String q = _for != null ? "?for=" + _for : "";
HttpURLConnection con = postJson(open("createVirtualMachine" + q), JSONObject.fromObject(spec,jsc));
verifyResponseStatus(con);
return new VirtualMachineRef(new URL(con.getHeaderField("Location")), token);
}
代码示例来源:origin: DozerMapper/dozer
/**
* Attempts to open a http connection for the systemId resource, and follows the first redirect
*
* @param systemId url to the XSD
* @return stream to XSD
* @throws IOException if fails to find XSD
*/
private InputStream resolveFromURL(String systemId) throws IOException {
LOG.debug("Trying to download [{}]", systemId);
URL obj = new URL(systemId);
HttpURLConnection conn = (HttpURLConnection)obj.openConnection();
int status = conn.getResponseCode();
if ((status != HttpURLConnection.HTTP_OK)
&& (status == HttpURLConnection.HTTP_MOVED_TEMP
|| status == HttpURLConnection.HTTP_MOVED_PERM
|| status == HttpURLConnection.HTTP_SEE_OTHER)) {
LOG.debug("Received status of {}, attempting to follow Location header", status);
String newUrl = conn.getHeaderField("Location");
conn = (HttpURLConnection)new URL(newUrl).openConnection();
}
return conn.getInputStream();
}
}
内容来源于网络,如有侵权,请联系作者删除!