本文整理匯總了Java中org.apache.commons.httpclient.HttpMethodBase.getResponseBodyAsStream方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpMethodBase.getResponseBodyAsStream方法的具體用法?Java HttpMethodBase.getResponseBodyAsStream怎麽用?Java HttpMethodBase.getResponseBodyAsStream使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.httpclient.HttpMethodBase
的用法示例。
在下文中一共展示了HttpMethodBase.getResponseBodyAsStream方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: executeMethod
import org.apache.commons.httpclient.HttpMethodBase; //導入方法依賴的package包/類
private static byte[] executeMethod(HttpMethodBase method, int timeout) throws Exception {
InputStream in = null;
try {
method.addRequestHeader("Connection", "close");
HttpClient client = new HttpClient();
HttpConnectionManagerParams params = client.getHttpConnectionManager().getParams();
//設置連接時候一些參數
params.setConnectionTimeout(timeout);
params.setSoTimeout(timeout);
params.setStaleCheckingEnabled(false);
ByteArrayOutputStream baos = new ByteArrayOutputStream(BUFFER_SIZE);
int stat = client.executeMethod(method);
if (stat != HttpStatus.SC_OK)
log.error("get失敗!");
//method.getResponseBody()
in = method.getResponseBodyAsStream();
byte[] buffer = new byte[BUFFER_SIZE];
int len;
while ((len = in.read(buffer)) > 0) {
baos.write(buffer, 0, len);
}
return baos.toByteArray();
}
finally {
if (in != null) {
in.close();
}
}
}
示例2: validate
import org.apache.commons.httpclient.HttpMethodBase; //導入方法依賴的package包/類
@Override
public boolean validate(HttpMethodBase method, NegotiationContext ctx)
{
InputStream is = null;
try {
is = method.getResponseBodyAsStream();
Model m = ModelFactory.createDefaultModel();
m.read(is, null, _lang);
}
catch (Throwable t) {
ctx.newContentError(ctx.getResource(), t);
return false;
}
finally { IOUtils.closeQuietly(is); }
return true;
}
示例3: createConnectionReleasingInputStream
import org.apache.commons.httpclient.HttpMethodBase; //導入方法依賴的package包/類
private InputStream createConnectionReleasingInputStream(final HttpMethodBase method) throws IOException {
return new FilterInputStream(method.getResponseBodyAsStream()) {
public void close() throws IOException {
try {
super.close();
} finally {
method.releaseConnection();
}
}
};
}
示例4: execute
import org.apache.commons.httpclient.HttpMethodBase; //導入方法依賴的package包/類
/**
* Execute the given Http method (GET, POST...) to the given URI and use the given processor to get the result.
*
* @param client HttpClient to use
* @param method http method to execute
* @param resultProcessor stream processor to use to consume HTTP response
* @return true if successful
* @throws IOException if any I/O operation fails (HTTP or file)
*/
private static boolean execute(final HttpClient client,
final HttpMethodBase method, final StreamProcessor resultProcessor) throws IOException {
try {
// memorize HTTPMethodBase associated to the current thread:
HttpMethodThreadMap.setCurrentThread(method);
// Send HTTP query
final int resultCode = client.executeMethod(method);
if (_logger.isDebugEnabled()) {
_logger.debug("The query has been sent. Status code: {}", resultCode);
}
// If everything went fine
if (resultCode == HttpStatus.SC_OK) {
// Get response
final InputStream in = new BufferedInputStream(method.getResponseBodyAsStream());
resultProcessor.process(in);
return true;
}
} finally {
// Release the connection.
releaseConnection(method);
}
return false;
}
示例5: getResponseAsStringAndHandleGzip
import org.apache.commons.httpclient.HttpMethodBase; //導入方法依賴的package包/類
public static String getResponseAsStringAndHandleGzip(HttpMethodBase httpget) throws IOException {
Header contentEncodingHeader = httpget.getResponseHeader(CONTENT_ENCODING_HEADER);
InputStream stream = httpget.getResponseBodyAsStream();
if (contentEncodingHeader != null && contentEncodingHeader.getValue().equalsIgnoreCase(GZIP)) {
stream = new GZIPInputStream(stream);
}
String inputStreamString = new Scanner(stream, "UTF-8").useDelimiter("\\A").next();
return inputStreamString;
}
示例6: HttpClientReleasingInputStream
import org.apache.commons.httpclient.HttpMethodBase; //導入方法依賴的package包/類
private HttpClientReleasingInputStream(HttpMethodBase httpRequest) throws IOException
{
super(httpRequest.getResponseBodyAsStream());
this.httpRequest = httpRequest;
}
示例7: executeMethod
import org.apache.commons.httpclient.HttpMethodBase; //導入方法依賴的package包/類
/**
* 執行post或者get方法
*
* @param method post或者get方法
* @return
* @throws java.io.IOException
*/
private static String executeMethod(HttpMethodBase method) throws IOException {
HttpClient httpClient = new HttpClient();
try {
int statusCode = httpClient.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
throw new IOException("Method failed: " + method.getStatusLine());
}
// get response stream.
InputStream stream = method.getResponseBodyAsStream();
// charset
String charset = method.getRequestCharSet();
String response;
try {
// try to turn stream to bytes.
byte[] responseBytes = IOUtils.toByteArray(stream);
// decode with the reponse charset.
if (StringUtils.isNotBlank(charset)) {
try {
response = new String(responseBytes, charset);
} catch (UnsupportedEncodingException e) {
logger.error(e.getMessage(), e);
response = new String(responseBytes);
}
} else {
// decode with default charset.
response = new String(responseBytes);
}
return response;
} finally {
// close stream.
IOUtils.closeQuietly(stream);
}
} finally {
// release connection.
method.releaseConnection();
}
}
示例8: processResponse
import org.apache.commons.httpclient.HttpMethodBase; //導入方法依賴的package包/類
protected void processResponse(HttpMethodBase httpMethod,
MessageContext msgContext)
throws IOException {
obtainHTTPHeaderInformation(httpMethod, msgContext);
InputStream in = httpMethod.getResponseBodyAsStream();
if (in == null) {
if (hasResponseBodyForHTTPStatusCode(httpMethod.getStatusCode())) {
throw new AxisFault(Messages.getMessage("canNotBeNull", "InputStream"));
} else {
in = new ByteArrayInputStream("".getBytes());
}
}
Header contentEncoding =
httpMethod.getResponseHeader(HTTPConstants.HEADER_CONTENT_ENCODING);
if (contentEncoding != null) {
if (contentEncoding.getValue().
equalsIgnoreCase(HTTPConstants.COMPRESSION_GZIP)) {
in = new GZIPInputStream(in);
// If the content-encoding is identity we can basically ignore it.
} else if (!"identity".equalsIgnoreCase(contentEncoding.getValue())) {
throw new AxisFault("HTTP :" + "unsupported content-encoding of '"
+ contentEncoding.getValue() + "' found");
}
}
if (httpMethod.getStatusCode() == HttpStatus.SC_ACCEPTED) {
Header contentLength = httpMethod.getResponseHeader(HTTPConstants.HEADER_CONTENT_LENGTH);
boolean acceptedContainsBody = false;
if (in.available() > 0 || (contentLength != null && Integer.parseInt(contentLength.getValue()) > 0)) {
acceptedContainsBody = true;
} else {
Header transferEncoding = httpMethod.getResponseHeader(HTTPConstants.HEADER_TRANSFER_ENCODING);
if (transferEncoding != null && transferEncoding.getValue().equals(
HTTPConstants.HEADER_TRANSFER_ENCODING_CHUNKED)) {
String responseBody = httpMethod.getResponseBodyAsString();
if (responseBody.length() > 0) {
acceptedContainsBody = true;
in = IOUtils.toInputStream(responseBody, "UTF-8");
}
}
}
if (!acceptedContainsBody) {
/*
* For HTTP 202 Accepted code, if the body is empty string then release connection.
*/
httpMethod.releaseConnection();
return;
}
}
OperationContext opContext = msgContext.getOperationContext();
if (opContext != null) {
opContext.setProperty(MessageContext.TRANSPORT_IN, in);
}
}