本文整理了Java中org.apache.commons.httpclient.URI
类的一些代码示例,展示了URI
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。URI
类的具体详情如下:
包路径:org.apache.commons.httpclient.URI
类名称:URI
[英]The interface for the URI(Uniform Resource Identifiers) version of RFC 2396. This class has the purpose of supportting of parsing a URI reference to extend any specific protocols, the character encoding of the protocol to be transported and the charset of the document.
A URI is always in an "escaped" form, since escaping or unescaping a completed URI might change its semantics.
Implementers should be careful not to escape or unescape the same string more than once, since unescaping an already unescaped string might lead to misinterpreting a percent data character as another escaped character, or vice versa in the case of escaping an already escaped string.
In order to avoid these problems, data types used as follows:
URI character sequence: char
octet sequence: byte
original character sequence: String
So, a URI is a sequence of characters as an array of a char type, which is not always represented as a sequence of octets as an array of byte.
URI Syntactic Components
- In general, written as follows:
Absolute URI = <scheme>:<scheme-specific-part>
Generic URI = <scheme>://<authority><path>?<query>
- Syntax
absoluteURI = scheme ":" ( hier_part | opaque_part )
hier_part = ( net_path | abs_path ) [ "?" query ]
net_path = "//" authority [ abs_path ]
abs_path = "/" path_segments
The following examples illustrate URI that are in common use.
ftp://ftp.is.co.za/rfc/rfc1808.txt
-- ftp scheme for File Transfer Protocol services
gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles
-- gopher scheme for Gopher and Gopher+ Protocol services
http://www.math.uio.no/faq/compression-faq/part1.html
-- http scheme for Hypertext Transfer Protocol services
mailto:mduerst@ifi.unizh.ch
-- mailto scheme for electronic mail addresses
news:comp.infosystems.www.servers.unix
-- news scheme for USENET news groups and articles
telnet://melvyl.ucop.edu/
-- telnet scheme for interactive services via the TELNET Protocol
Please, notice that there are many modifications from URL(RFC 1738) and relative URL(RFC 1808).
The expressions for a URI
For escaped URI forms
- URI(char[]) // constructor
- char[] getRawXxx() // method
- String getEscapedXxx() // method
- String toString() // method
For unescaped URI forms
- URI(String) // constructor
- String getXXX() // method
[中]RFC 2396的URI(统一资源标识符)版本的接口。此类的目的是支持解析URI引用以扩展任何特定协议、要传输的协议的字符编码和文档的字符集。
URI总是以“转义”形式出现,因为转义或取消转义已完成的URI可能会改变其语义。
实现人员应该小心,不要多次转义或取消转义同一字符串,因为取消转义已经未转义的字符串可能会导致将百分比数据字符误解为另一个转义字符,或者在转义已经转义的字符串的情况下,将百分比数据字符误解为另一个转义字符。
为了避免这些问题,使用的数据类型如下:
URI character sequence: char
octet sequence: byte
original character sequence: String
因此,URI是一个字符序列,作为一个字符类型的数组,它并不总是表示为一个字节数组的八位字节序列。
URI语法组件
- In general, written as follows:
Absolute URI = <scheme>:<scheme-specific-part>
Generic URI = <scheme>://<authority><path>?<query>
- Syntax
absoluteURI = scheme ":" ( hier_part | opaque_part )
hier_part = ( net_path | abs_path ) [ "?" query ]
net_path = "//" authority [ abs_path ]
abs_path = "/" path_segments
以下示例说明了常用的URI。
ftp://ftp.is.co.za/rfc/rfc1808.txt
-- ftp scheme for File Transfer Protocol services
gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles
-- gopher scheme for Gopher and Gopher+ Protocol services
http://www.math.uio.no/faq/compression-faq/part1.html
-- http scheme for Hypertext Transfer Protocol services
mailto:mduerst@ifi.unizh.ch
-- mailto scheme for electronic mail addresses
news:comp.infosystems.www.servers.unix
-- news scheme for USENET news groups and articles
telnet://melvyl.ucop.edu/
-- telnet scheme for interactive services via the TELNET Protocol
请注意,URL(RFC 1738)和相对URL(RFC 1808)有很多修改。
URI的表达式
For escaped URI forms
- URI(char[]) // constructor
- char[] getRawXxx() // method
- String getEscapedXxx() // method
- String toString() // method
For unescaped URI forms
- URI(String) // constructor
- String getXXX() // method
代码示例来源:origin: elastic/elasticsearch-hadoop
break;
case GET:
http = (request.body() == null ? new GetMethod() : new GetMethodWithBody());
break;
case POST:
throw new EsHadoopInvalidRequest("URI has query portion on it: [" + uri + "]");
http.setURI(new URI(escapeUri(uri.toString(), sslEnabled), false));
http.setPath(path);
uri = http.getURI().toString();
} catch (URIException uriex) {
throw new EsHadoopTransportException("Invalid target URI " + request, uriex);
client.executeMethod(http);
} finally {
stats.netTotalTime += (System.currentTimeMillis() - start);
代码示例来源:origin: apache/incubator-pinot
try {
getMethod = completionService.take().get();
URI uri = getMethod.getURI();
String instance = endpointsToServers.get(uri.getHost() + ":" + uri.getPort());
if (getMethod.getStatusCode() >= 300) {
LOGGER.error("Server: {} returned error: {}", instance, getMethod.getStatusCode());
continue;
代码示例来源:origin: commons-httpclient/commons-httpclient
throws RedirectException {
Header locationHeader = method.getResponseHeader("location");
if (locationHeader == null) {
currentUri = new URI(
this.conn.getProtocol().getScheme(),
null,
this.conn.getHost(),
this.conn.getPort(),
method.getPath()
);
String charset = method.getParams().getUriCharset();
redirectUri = new URI(location, true, charset);
if (redirectUri.isRelativeURI()) {
if (this.params.isParameterTrue(HttpClientParams.REJECT_RELATIVE_REDIRECT)) {
LOG.warn("Relative redirect location '" + location + "' not allowed");
redirectUri = new URI(currentUri, redirectUri);
if(redirectUri.hasQuery()) {
redirectUri.setQuery(null);
LOG.debug("Redirecting from '" + currentUri.getEscapedURI()
+ "' to '" + redirectUri.getEscapedURI());
代码示例来源:origin: commons-httpclient/commons-httpclient
/**
* Compare this URI to another object.
*
* @param obj the object to be compared.
* @return 0, if it's same,
* -1, if failed, first being compared with in the authority component
* @throws ClassCastException not URI argument
*/
public int compareTo(Object obj) throws ClassCastException {
URI another = (URI) obj;
if (!equals(_authority, another.getRawAuthority())) {
return -1;
}
return toString().compareTo(another.toString());
}
代码示例来源:origin: commons-httpclient/commons-httpclient
/**
* URI constructor for HttpHost.
*
* @param uri the URI.
*/
public HttpHost(final URI uri) throws URIException {
this(uri.getHost(), uri.getPort(), Protocol.getProtocol(uri.getScheme()));
}
代码示例来源:origin: commons-httpclient/commons-httpclient
/**
* Sets the protocol, host and port from the given URI.
* @param uri the URI.
*/
public synchronized void setHost(final URI uri) {
try {
setHost(uri.getHost(), uri.getPort(), uri.getScheme());
} catch (URIException e) {
throw new IllegalArgumentException(e.toString());
}
}
代码示例来源:origin: com.facebook.hadoop/hadoop-core
private static int httpNotification(String uri) throws IOException {
URI url = new URI(uri, false);
HttpClient m_client = new HttpClient();
HttpMethod method = new GetMethod(url.getEscapedURI());
method.setRequestHeader("Accept", "*/*");
return m_client.executeMethod(method);
}
代码示例来源:origin: apache/incubator-wave
private JsonArray sendSearchRequest(String solrQuery,
Function<InputStreamReader, JsonArray> function) throws IOException {
JsonArray docsJson;
GetMethod getMethod = new GetMethod();
HttpClient httpClient = new HttpClient();
try {
getMethod.setURI(new URI(solrQuery, false));
int statusCode = httpClient.executeMethod(getMethod);
docsJson = function.apply(new InputStreamReader(getMethod.getResponseBodyAsStream()));
if (statusCode != HttpStatus.SC_OK) {
LOG.warning("Failed to execute query: " + solrQuery);
throw new IOException("Search request status is not OK: " + statusCode);
}
} finally {
getMethod.releaseConnection();
}
return docsJson;
}
代码示例来源:origin: org.netpreserve.commons/webarchive-commons
activeMethod = new GetMethod(url);
activeMethod.setRequestHeader("Range", rangeHeader);
activeMethod.setRequestHeader("Connection", "close");
int code = http.executeMethod(activeMethod);
connectedUrl = activeMethod.getURI().toString();
connectedUrl = activeMethod.getURI().toString();
doClose();
throw io;
代码示例来源:origin: com.atlassian.ext/atlassian-plugin-repository-confluence-plugin
protected static String retrieveXML(String url)
{
HttpClient httpClient = new HttpClient();
String responseXML = "";
GetMethod get = null;
try
{
URI uri = new URI(url, false);
String escapedUrl = uri.getEscapedURI();
get = new GetMethod(escapedUrl);
httpClient.executeMethod(get);
responseXML = get.getResponseBodyAsString();
}
catch (Exception e)
{
responseXML = "<errors><error>Error retreiving XML.</error></errors>"; // *** need i18n here
}
finally
{
get.releaseConnection();
}
return responseXML;
}
代码示例来源:origin: org.apache.any23/apache-any23-core
String uriStr;
try {
URI uriObj = new URI(uri, isUrlEncoded(uri));
uriStr = uriObj.toString();
} catch (URIException e) {
throw new IllegalArgumentException("Invalid IRI string.", e);
method = new GetMethod(uriStr);
method.setFollowRedirects(true);
client.executeMethod(method);
_contentLength = method.getResponseContentLength();
final Header contentTypeHeader = method.getResponseHeader("Content-Type");
contentType = contentTypeHeader == null ? null : contentTypeHeader.getValue();
);
actualDocumentIRI = method.getURI().toString();
byte[] response = method.getResponseBody();
代码示例来源:origin: geotools/geotools
/**
* @param method
* @return the http status code of the execution
* @throws IOException
* @throws HttpException
*/
private int executeMethod(HttpMethod method) throws IOException, HttpException {
String host = method.getURI().getHost();
if (host != null && nonProxyHosts.contains(host.toLowerCase())) {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine(
"Bypassing proxy config due to nonProxyHosts for "
+ method.getURI().toString());
}
return client.executeMethod(hostConfigNoProxy, method);
}
return client.executeMethod(method);
}
代码示例来源:origin: mguessan/davmail
/**
* Execute method, redirect once if returned status is redirect.
*
* @param httpClient http client
* @param method http method
* @return status
* @throws IOException on error
*/
protected static int executeMethodFollowRedirectOnce(HttpClient httpClient, HttpMethod method) throws IOException {
int status = httpClient.executeMethod(method);
// need to follow redirects (once) on public folders
if (isRedirect(status)) {
method.releaseConnection();
URI targetUri = new URI(method.getResponseHeader("Location").getValue(), true);
checkExpiredSession(targetUri.getQuery());
method.setURI(targetUri);
status = httpClient.executeMethod(method);
}
return status;
}
代码示例来源:origin: elastic/elasticsearch-hadoop
hostConfig.setHost(new URI(escapeUri(host, sslEnabled), false));
} catch (IOException ex) {
throw new EsHadoopTransportException("Invalid target URI " + host, ex);
client = new HttpClient(params, new SocketTrackingConnectionManager());
client.setHostConfiguration(hostConfig);
HttpConnectionManagerParams connectionParams = client.getHttpConnectionManager().getParams();
代码示例来源:origin: eclipse/winery
/**
* Executes a passed HttpMethod (Method type is either PUT, POST, GET or
* DELETE) and returns a HttpResponseMessage
*
* @param method Method to execute
* @return HttpResponseMessage which contains all information about the
* execution
*/
public static HttpResponseMessage executeHttpMethod(HttpMethod method) {
HttpResponseMessage responseMessage = null;
try {
System.out.println("Method invocation on URI: \n");
System.out.println(method.getURI().toString());
// Execute Request
LowLevelRestApi.httpClient.executeMethod(method);
responseMessage = LowLevelRestApi.extractResponseInformation(method);
} catch (Exception e) {
e.printStackTrace();
} finally {
// Release Connection anyway
method.releaseConnection();
}
// Extract response information and return
return responseMessage;
}
代码示例来源:origin: co.cask.hbase/hbase
/**
* Execute a transaction method given a complete URI.
* @param method the transaction method
* @param headers HTTP header values to send
* @param uri a properly urlencoded URI
* @return the HTTP response code
* @throws IOException
*/
public int executeURI(HttpMethod method, Header[] headers, String uri)
throws IOException {
method.setURI(new URI(uri, true));
for (Map.Entry<String, String> e: extraHeaders.entrySet()) {
method.addRequestHeader(e.getKey(), e.getValue());
}
if (headers != null) {
for (Header header: headers) {
method.addRequestHeader(header);
}
}
long startTime = System.currentTimeMillis();
int code = httpClient.executeMethod(method);
long endTime = System.currentTimeMillis();
if (LOG.isDebugEnabled()) {
LOG.debug(method.getName() + " " + uri + " " + code + " " +
method.getStatusText() + " in " + (endTime - startTime) + " ms");
}
return code;
}
代码示例来源:origin: com.codeslap/github-jobs-java-api
private static String createUrl(String url, List<NameValuePair> pairs) throws URIException {
HttpMethod method = new GetMethod(url);
NameValuePair[] nameValuePairs = pairs.toArray(new NameValuePair[pairs.size()]);
method.setQueryString(nameValuePairs);
return method.getURI().getEscapedURI();
}
}
代码示例来源:origin: org.motechproject/motech-openmrs-atomfeed
@Autowired
public OpenMrsHttpClientImpl(@Value("${openmrs.url}") String openmrsUrl) throws URIException {
Validate.notEmpty(
openmrsUrl,
"Did not find property for OpenMRS Url (openmrs.url). Cannot use the Motech Atom Feed module until this property is set");
httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
URI uri = new URI(openmrsUrl, false);
openmrsPath = uri.getPath();
httpClient.getHostConfiguration().setHost(uri);
}
代码示例来源:origin: foxinmy/weixin4j
org.apache.commons.httpclient.HttpMethod httpMethod = null;
try {
URI uri = new URI(request.getURI().toString(), false,
Consts.UTF_8.name());
if (method == HttpMethod.GET) {
httpMethod = new GetMethod();
} else if (method == HttpMethod.HEAD) {
httpMethod = new HeadMethod();
httpMethod = new OptionsMethod();
} else if (method == HttpMethod.TRACE) {
return new TraceMethod(uri.getEscapedURI());
} else {
throw new HttpClientException("unknown request method "
+ method + " for " + uri);
httpMethod.setURI(uri);
} catch (IOException e) {
throw new HttpClientException("I/O error on " + method.name()
代码示例来源:origin: org.alfresco/alfresco-core
/**
* Send Request to the repository
*/
protected HttpMethod sendRemoteRequest(Request req) throws AuthenticationException, IOException
{
if (logger.isDebugEnabled())
{
logger.debug("");
logger.debug("* Request: " + req.getMethod() + " " + req.getFullUri() + (req.getBody() == null ? "" : "\n" + new String(req.getBody(), "UTF-8")));
}
HttpMethod method = createMethod(req);
// execute method
executeMethod(method);
// Deal with redirect
if(isRedirect(method))
{
Header locationHeader = method.getResponseHeader("location");
if (locationHeader != null)
{
String redirectLocation = locationHeader.getValue();
method.setURI(new URI(redirectLocation, true));
httpClient.executeMethod(method);
}
}
return method;
}
内容来源于网络,如有侵权,请联系作者删除!