下面列出了java.net.HttpURLConnection#getErrorStream ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void doTestIllegalPathParam() throws IOException {
String paramString = "%%A";
String requestUri = client.getUrlPrefix() + "/intPath/" + paramString;
URL url = new URL(requestUri);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
String errorBody = null;
int responseCode = urlConnection.getResponseCode();
String responseMessage = urlConnection.getResponseMessage();
try (Scanner scanner = new Scanner(urlConnection.getErrorStream())) {
errorBody = scanner.nextLine();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
urlConnection.disconnect();
assertEquals(400, responseCode);
assertEquals("Bad Request", responseMessage);
assertEquals("Bad Request", errorBody);
}
private static BitbucketException fault(final HttpURLConnection http) throws IOException {
final int responseCode = http.getResponseCode();
try (final InputStream stream =
(responseCode >= 400 ? http.getErrorStream() : http.getInputStream())) {
String body = null;
if (stream != null) {
final int length = http.getContentLength();
body = readBody(stream, length);
}
return new BitbucketException(responseCode, body, http.getContentType());
}
}
public static String getFromHTTP(String url, Time timeout) throws Exception {
final URL u = new URL(url);
LOG.info("Accessing URL " + url + " as URL: " + u);
final long deadline = timeout.toMilliseconds() + System.currentTimeMillis();
while (System.currentTimeMillis() <= deadline) {
HttpURLConnection connection = (HttpURLConnection) u.openConnection();
connection.setConnectTimeout(100000);
connection.connect();
if (Objects.equals(HttpResponseStatus.SERVICE_UNAVAILABLE, HttpResponseStatus.valueOf(connection.getResponseCode()))) {
// service not available --> Sleep and retry
LOG.debug("Web service currently not available. Retrying the request in a bit.");
Thread.sleep(100L);
} else {
InputStream is;
if (connection.getResponseCode() >= 400) {
// error!
LOG.warn("HTTP Response code when connecting to {} was {}", url, connection.getResponseCode());
is = connection.getErrorStream();
} else {
is = connection.getInputStream();
}
return IOUtils.toString(is, ConfigConstants.DEFAULT_CHARSET);
}
}
throw new TimeoutException("Could not get HTTP response in time since the service is still unavailable.");
}
@NonNull
private static InputStream getInputStreamFrom(
@NonNull HttpURLConnection httpURLConnection) throws IOException {
int httpResponseCode = httpURLConnection.getResponseCode();
InputStream inputStream = httpResponseCode < 400
? httpURLConnection.getInputStream()
: httpURLConnection.getErrorStream();
inputStream = isGzipUsed(httpURLConnection)
? new GZIPInputStream(inputStream)
: inputStream;
return inputStream;
}
protected static String getResponseAsString(HttpURLConnection conn) throws IOException {
String charset = getResponseCharset(conn.getContentType());
InputStream es = conn.getErrorStream();
if (es == null) {
return getStreamAsString(conn.getInputStream(), charset);
} else {
String msg = getStreamAsString(es, charset);
if (StringUtils.isEmpty(msg)) {
throw new IOException(conn.getResponseCode() + ":" + conn.getResponseMessage());
} else {
throw new IOException(msg);
}
}
}
private void parseResponse(@NonNull HttpURLConnection conn) throws IOException {
Preconditions.checkNotNull(conn);
resultCode = conn.getResponseCode();
resultHeaders = conn.getHeaderFields();
resultingContentLength = conn.getContentLength();
if (isResultSuccess()) {
resultInputStream = conn.getInputStream();
} else {
resultInputStream = conn.getErrorStream();
}
}
/**
* Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
* @param connection
* @return an HttpEntity populated with data from <code>connection</code>.
*/
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
BasicHttpEntity entity = new BasicHttpEntity();
InputStream inputStream;
try {
inputStream = connection.getInputStream();
} catch (IOException ioe) {
inputStream = connection.getErrorStream();
}
entity.setContent(inputStream);
entity.setContentLength(connection.getContentLength());
entity.setContentEncoding(connection.getContentEncoding());
entity.setContentType(connection.getContentType());
return entity;
}
static void printHttpError(
HttpURLConnection connection
) {
InputStream is = connection.getErrorStream();
if( is == null ) return;
try
{
System.out.println( ioStream2String( is ) );
}
catch( IOException e )
{
e.printStackTrace();
}
}
private String getErrorMessage(IOException exception, HttpURLConnection connection) {
String message = exception.getMessage();
try (InputStream error = connection.getErrorStream(); JsonReader rdr = Json.createReader(error)) {
JsonObject obj = rdr.readObject();
message = obj.getString("message");
} catch (Exception e) {
// ignore. likely there is no response
}
return message;
}
private static ResponseBody createResponseBody(HttpURLConnection urlConnection) throws IOException {
long contentLength = urlConnection.getContentLength();
MediaType type = URLMediaType.parse(urlConnection.getContentType());
InputStream stream;
try {
stream = urlConnection.getInputStream();
} catch (IOException ioe) {
stream = urlConnection.getErrorStream();
}
return new ResponseBodyImpl(stream, type, contentLength);
}
private static byte[] download(String path, String hash, IntConsumer progress) throws IOException, VerificationException
{
HashFunction hashFunction = Hashing.sha256();
Hasher hasher = hashFunction.newHasher();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.getResponseCode();
InputStream err = conn.getErrorStream();
if (err != null)
{
err.close();
throw new IOException("Unable to download " + path + " - " + conn.getResponseMessage());
}
int downloaded = 0;
try (InputStream in = conn.getInputStream())
{
int i;
byte[] buffer = new byte[1024 * 1024];
while ((i = in.read(buffer)) != -1)
{
byteArrayOutputStream.write(buffer, 0, i);
hasher.putBytes(buffer, 0, i);
downloaded += i;
progress.accept(downloaded);
}
}
HashCode hashCode = hasher.hash();
if (!hash.equals(hashCode.toString()))
{
throw new VerificationException("Unable to verify resource " + path + " - expected " + hash + " got " + hashCode.toString());
}
return byteArrayOutputStream.toByteArray();
}
protected String issueProfileDelete(String path, String ifMatchEtagValue)
throws java.io.IOException {
URL url = new URL(this._host.getProtocol(), this._host.getHost(), this._host.getPort(), this._host.getPath()+path);
HttpURLConnection conn = initializeConnection(url);
//Agent profile requires If-Match header - exception will get caught when making
conn.addRequestProperty("If-Match", ifMatchEtagValue);
conn.setRequestMethod("DELETE");
try{
return readFromConnection(conn);
}
catch (IOException ex){
InputStream s = conn.getErrorStream();
InputStreamReader isr = new InputStreamReader(s);
BufferedReader br = new BufferedReader(isr);
try {
String line;
while((line = br.readLine()) != null){
System.out.print(line);
}
System.out.println();
} finally {
s.close();
}
throw ex;
}
finally{
conn.disconnect();
}
}
private void callAPI(Object data, String uriSuffix,
OutputStream outStream, String contentType) {
try
{
HttpURLConnection conn = getConnection(uriSuffix, contentType);
OutputStream wr = conn.getOutputStream();
if (data instanceof byte[]) {
//System.out.println(new String((byte[])data));
wr.write((byte[])data);
}
else {
//System.out.println(((ByteArrayOutputStream)data).toString());
((ByteArrayOutputStream)data).writeTo(wr);
}
wr.flush();
if (conn.getResponseCode() == 200) {
InputStream rd = conn.getInputStream();
copyStream(rd, outStream);
rd.close();
}
else {
String errMsg;
if (conn.getErrorStream() != null) {
ByteArrayOutputStream errOut = new ByteArrayOutputStream();
copyStream(conn.getErrorStream(), errOut);
errMsg = errOut.toString();
}
else {
errMsg = conn.getResponseMessage();
}
throw new PdfcrowdError(errMsg, conn.getResponseCode());
}
wr.close();
}
catch(IOException e)
{
throw new PdfcrowdError(e);
}
}
/**
* Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
* @param connection
* @return an HttpEntity populated with data from <code>connection</code>.
*/
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
BasicHttpEntity entity = new BasicHttpEntity();
InputStream inputStream;
try {
inputStream = connection.getInputStream();
} catch (IOException ioe) {
inputStream = connection.getErrorStream();
}
entity.setContent(inputStream);
entity.setContentLength(connection.getContentLength());
entity.setContentEncoding(connection.getContentEncoding());
entity.setContentType(connection.getContentType());
return entity;
}
/**
* Makes a request to the specified Url and return the results as a String
*
* @param requestUrl url to make request to
* @return the XML document as a String
* @throws IOException
*/
public static String makeRequest(String requestUrl, String authorization, String amzDate) throws IOException {
URL url = new URL(requestUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Accept", "application/xml");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("X-Amz-Date", amzDate);
conn.setRequestProperty("Authorization", authorization);
InputStream in = (conn.getResponseCode() / 100 == 2 ? conn.getInputStream() : conn.getErrorStream());
// Read the response
BufferedReader replyReader =
new BufferedReader
(new InputStreamReader
(conn.getInputStream()));
String line;
String replyString = "";
while ((line = replyReader.readLine()) != null) {
replyString = replyString.concat(line + "\n");
}
replyReader.close();
return replyString;
}
@Test
public void testResponseHeaders() throws Exception {
// check headers for successful json response
URL taskManagersUrl = new URL("http://localhost:" + getRestPort() + "/taskmanagers");
HttpURLConnection taskManagerConnection = (HttpURLConnection) taskManagersUrl.openConnection();
taskManagerConnection.setConnectTimeout(100000);
taskManagerConnection.connect();
if (taskManagerConnection.getResponseCode() >= 400) {
// error!
InputStream is = taskManagerConnection.getErrorStream();
String errorMessage = IOUtils.toString(is, ConfigConstants.DEFAULT_CHARSET);
fail(errorMessage);
}
// we don't set the content-encoding header
Assert.assertNull(taskManagerConnection.getContentEncoding());
Assert.assertEquals("application/json; charset=UTF-8", taskManagerConnection.getContentType());
// check headers in case of an error
URL notFoundJobUrl = new URL("http://localhost:" + getRestPort() + "/jobs/dontexist");
HttpURLConnection notFoundJobConnection = (HttpURLConnection) notFoundJobUrl.openConnection();
notFoundJobConnection.setConnectTimeout(100000);
notFoundJobConnection.connect();
if (notFoundJobConnection.getResponseCode() >= 400) {
// we don't set the content-encoding header
Assert.assertNull(notFoundJobConnection.getContentEncoding());
Assert.assertEquals("application/json; charset=UTF-8", notFoundJobConnection.getContentType());
} else {
fail("Request for non-existing job did not return an error.");
}
}
public synchronized byte[] getAttachmentData(String uri, Map<String,Object> headers) throws IOException, OslcException {
if(!isValid()){
throw new OslcException("The instance of MaximoConnector is not valid.");
}
String publicHost = this.options.getHost();
if(this.options.getPort()!=-1){
publicHost+= ":" + String.valueOf(this.options.getPort());
}
if(!uri.contains(publicHost)){
URL tempURL = new URL(uri);
String currentHost = tempURL.getHost();
if(tempURL.getPort()!=-1){
currentHost+= ":" + String.valueOf(tempURL.getPort());
}
uri = uri.replace(currentHost, publicHost);
}
//LOG.isLoggable(Level.info);
logger.fine(uri);
URL httpURL = new URL(uri);
HttpURLConnection con = (HttpURLConnection) httpURL
.openConnection();
con = this.setMethod(con, "GET");
if (headers!=null && !headers.isEmpty() ) {
con = this.setHeaders(con, headers);
}
if (cookies == null)
this.connect();
this.setCookiesForSession(con);
int resCode = con.getResponseCode();
lastResponseCode = resCode;
InputStream inStream = null;
if (resCode >= 400) {
inStream = con.getErrorStream();
JsonReader rdr = Json.createReader(inStream);
JsonObject obj = rdr.readObject();
throw new OslcException(obj);
} else {
inStream = con.getInputStream();
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[0xFFFF];
for (int len; (len = inStream.read(buffer)) != -1;)
bos.write(buffer, 0, len);
bos.flush();
return bos.toByteArray();
}
static Response execute(Connection.Request req, Response previousResponse) throws IOException {
Validate.notNull(req, "Request must not be null");
String protocol = req.url().getProtocol();
if (!protocol.equals("http") && !protocol.equals("https"))
throw new MalformedURLException("Only http & https protocols supported");
final boolean methodHasBody = req.method().hasBody();
final boolean hasRequestBody = req.requestBody() != null;
if (!methodHasBody)
Validate.isFalse(hasRequestBody, "Cannot set a request body for HTTP method " + req.method());
// set up the request for execution
String mimeBoundary = null;
if (req.data().size() > 0 && (!methodHasBody || hasRequestBody))
serialiseRequestUrl(req);
else if (methodHasBody)
mimeBoundary = setOutputContentType(req);
HttpURLConnection conn = createConnection(req);
Response res;
try {
conn.connect();
if (conn.getDoOutput())
writePost(req, conn.getOutputStream(), mimeBoundary);
int status = conn.getResponseCode();
res = new Response(previousResponse);
res.setupFromConnection(conn, previousResponse);
res.req = req;
// redirect if there's a location header (from 3xx, or 201 etc)
if (res.hasHeader(LOCATION) && req.followRedirects()) {
if (status != HTTP_TEMP_REDIR) {
req.method(Method.GET); // always redirect with a get. any data param from original req are dropped.
req.data().clear();
req.requestBody(null);
req.removeHeader(CONTENT_TYPE);
}
String location = res.header(LOCATION);
if (location != null && location.startsWith("http:/") && location.charAt(6) != '/') // fix broken Location: http:/temp/AAG_New/en/index.php
location = location.substring(6);
URL redir = StringUtil.resolve(req.url(), location);
req.url(encodeUrl(redir));
for (Map.Entry<String, String> cookie : res.cookies.entrySet()) { // add response cookies to request (for e.g. login posts)
req.cookie(cookie.getKey(), cookie.getValue());
}
return execute(req, res);
}
if ((status < 200 || status >= 400) && !req.ignoreHttpErrors())
throw new HttpStatusException("HTTP error fetching URL", status, req.url().toString());
// check that we can handle the returned content type; if not, abort before fetching it
String contentType = res.contentType();
if (contentType != null
&& !req.ignoreContentType()
&& !contentType.startsWith("text/")
&& !xmlContentTypeRxp.matcher(contentType).matches()
)
throw new UnsupportedMimeTypeException("Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml",
contentType, req.url().toString());
// switch to the XML parser if content type is xml and not parser not explicitly set
if (contentType != null && xmlContentTypeRxp.matcher(contentType).matches()) {
// only flip it if a HttpConnection.Request (i.e. don't presume other impls want it):
if (req instanceof HttpConnection.Request && !((Request) req).parserDefined) {
req.parser(Parser.xmlParser());
}
}
res.charset = DataUtil.getCharsetFromContentType(res.contentType); // may be null, readInputStream deals with it
if (conn.getContentLength() != 0 && req.method() != HEAD) { // -1 means unknown, chunked. sun throws an IO exception on 500 response with no content when trying to read body
res.bodyStream = null;
res.bodyStream = conn.getErrorStream() != null ? conn.getErrorStream() : conn.getInputStream();
if (res.hasHeaderWithValue(CONTENT_ENCODING, "gzip"))
res.bodyStream = new GZIPInputStream(res.bodyStream);
res.bodyStream = new ConstrainableInputStream(res.bodyStream, DataUtil.bufferSize, req.maxBodySize());
} else {
res.byteData = DataUtil.emptyByteBuffer();
}
} catch (IOException e){
// per Java's documentation, this is not necessary, and precludes keepalives. However in practise,
// connection errors will not be released quickly enough and can cause a too many open files error.
conn.disconnect();
throw e;
}
res.executed = true;
return res;
}
private String sendStatementWithAttachment(HttpURLConnection conn, String data, String boundary, ArrayList<AttachmentAndType> attachmentData) throws java.io.IOException, NoSuchAlgorithmException {
OutputStreamWriter writer = new OutputStreamWriter(
conn.getOutputStream(), "UTF-8");
try {
writer.append("--" + boundary).append(LINE_FEED);
writer.append("Content-Type:application/json").append(LINE_FEED).append(LINE_FEED);
writer.append(data).append(LINE_FEED);
for (AttachmentAndType attachmentAndType : attachmentData) {
writer.append("--" + boundary).append(LINE_FEED);
writer.append("Content-Type:" + attachmentAndType.getType()).append(LINE_FEED);
writer.append("Content-Transfer-Encoding:binary").append(LINE_FEED);
writer.append("X-Experience-API-Hash:" + Attachment.generateSha2(attachmentAndType.getAttachment())).append(LINE_FEED).append(LINE_FEED);
writer.append(new String(attachmentAndType.getAttachment())).append(LINE_FEED);
}
writer.append("--" + boundary + "--");
writer.flush();
} catch (IOException ex) {
InputStream s = conn.getErrorStream();
InputStreamReader isr = new InputStreamReader(s);
BufferedReader br = new BufferedReader(isr);
try {
String line;
while((line = br.readLine()) != null){
System.out.print(line);
}
System.out.println();
} finally {
s.close();
}
throw ex;
} finally {
writer.close();
}
try {
return readFromConnection(conn);
} finally {
conn.disconnect();
}
}
/**
* Get an InputStream from the server response.
* Valid responses have response codes less than 400 (bad request).
* Valid responses are read from getInputStream(), while error responses must be read from getErrorStream()
* @param responseCode
* @param connection
* @return InputStream
* @throws IOException
*/
private InputStream getResponseStream(int responseCode, HttpURLConnection connection) throws IOException {
return (responseCode < HttpURLConnection.HTTP_BAD_REQUEST) ? connection.getInputStream() : connection.getErrorStream();
}