當前位置: 首頁>>代碼示例>>Java>>正文


Java HttpURLConnection.getErrorStream方法代碼示例

本文整理匯總了Java中java.net.HttpURLConnection.getErrorStream方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpURLConnection.getErrorStream方法的具體用法?Java HttpURLConnection.getErrorStream怎麽用?Java HttpURLConnection.getErrorStream使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.net.HttpURLConnection的用法示例。


在下文中一共展示了HttpURLConnection.getErrorStream方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: readAscii

import java.net.HttpURLConnection; //導入方法依賴的package包/類
/**
 * Reads {@code count} characters from the stream. If the stream is exhausted before {@code count}
 * characters can be read, the remaining characters are returned and the stream is closed.
 */
private String readAscii(URLConnection connection, int count) throws IOException {
  HttpURLConnection httpConnection = (HttpURLConnection) connection;
  InputStream in = httpConnection.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST
      ? connection.getInputStream()
      : httpConnection.getErrorStream();
  StringBuilder result = new StringBuilder();
  for (int i = 0; i < count; i++) {
    int value = in.read();
    if (value == -1) {
      in.close();
      break;
    }
    result.append((char) value);
  }
  return result.toString();
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:21,代碼來源:UrlConnectionCacheTest.java

示例2: processResponseOutput

import java.net.HttpURLConnection; //導入方法依賴的package包/類
private void processResponseOutput(HttpURLConnection con, Response response) throws IOException {
  InputStream in = con.getErrorStream();
  try {
    if (in == null) {
      in = con.getInputStream();
    }
    ByteArrayOutputStream result = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length;
    while ((length = in.read(buffer)) != -1) {
      result.write(buffer, 0, length);
    }
    response.output = result.toString();
  } finally {
    if (in != null) {
      in.close();
    }
  }
}
 
開發者ID:symphonyoss,項目名稱:JCurl,代碼行數:20,代碼來源:JCurl.java

示例3: getResponseAsResponseEntity

import java.net.HttpURLConnection; //導入方法依賴的package包/類
private static HttpResponseEntity getResponseAsResponseEntity(HttpURLConnection conn) throws IOException {
    HttpResponseEntity responseEntity = new HttpResponseEntity();
    String charset = getResponseCharset(conn.getContentType());
    InputStream es = conn.getErrorStream();

    responseEntity.setStatusCode(conn.getResponseCode());
    if (es == null) {
        String contentEncoding = conn.getContentEncoding();
        if (CONTENT_ENCODING_GZIP.equalsIgnoreCase(contentEncoding)) {
            responseEntity.setBody(getStreamAsString(new GZIPInputStream(conn.getInputStream()), charset));
        } else {
            responseEntity.setBody(getStreamAsString(conn.getInputStream(), charset));
        }
    } else {
        String msg = getStreamAsString(es, charset);
        if (StringUtils.isEmpty(msg)) {
            responseEntity.setBody(conn.getResponseCode() + ":" + conn.getResponseMessage());
        } else {
            responseEntity.setBody(msg);
        }
    }

    return responseEntity;
}
 
開發者ID:warlock-china,項目名稱:azeroth,代碼行數:25,代碼來源:HttpUtils.java

示例4: entityFromConnection

import java.net.HttpURLConnection; //導入方法依賴的package包/類
/**
 * 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;
}
 
開發者ID:pooyafaroka,項目名稱:PlusGram,代碼行數:20,代碼來源:HurlStack.java

示例5: getResponseAsString

import java.net.HttpURLConnection; //導入方法依賴的package包/類
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);
        }
    }
}
 
開發者ID:1991wangliang,項目名稱:pay,代碼行數:15,代碼來源:AlipayMobilePublicMultiMediaClient.java

示例6: GetJSON

import java.net.HttpURLConnection; //導入方法依賴的package包/類
private static JSONObject GetJSON(URL url) {
    try {
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestProperty("Accept", "application/json");
        urlConnection.setRequestProperty("Client-ID", "zdol60x6sxbx0phk60ci6mki79qhrf");
        urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0");
        urlConnection.setUseCaches(false);

        InputStream inputStream;
        int status = urlConnection.getResponseCode();

        if (status != HttpURLConnection.HTTP_OK)
            inputStream = urlConnection.getErrorStream();
        else
            inputStream = urlConnection.getInputStream();

        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            String inputLine = br.readLine();

            br.close();

            return new JSONObject(inputLine);
        } finally {
            urlConnection.disconnect();
        }
    } catch(Exception e) {
        e.printStackTrace();
    }

    return null;
}
 
開發者ID:invghost,項目名稱:NeoStream,代碼行數:33,代碼來源:TwitchAPI.java

示例7: readErrorFromConnection

import java.net.HttpURLConnection; //導入方法依賴的package包/類
private static NexusError readErrorFromConnection(HttpURLConnection connection) throws NexusCommunicationException {
    try {
        InputStream e = connection == null?null:connection.getErrorStream();
        if(e == null) {
            LogService.getRoot().log(Level.WARNING, "com.rapidminer.tools.account.RapidMinerAccount.error_parse_failure", "No available error stream.");
            throw new NexusCommunicationException(500);
        } else {
            return (NexusError)NexusUtilities.parseJacksonString(Tools.parseInputStreamToString(e), NexusError.class);
        }
    } catch (IOException var2) {
        LogService.getRoot().log(Level.WARNING, "com.rapidminer.tools.account.RapidMinerAccount.error_parse_failure", var2.getMessage());
        throw new NexusCommunicationException(500);
    }
}
 
開發者ID:transwarpio,項目名稱:rapidminer,代碼行數:15,代碼來源:RapidMinerAccount.java

示例8: manageError

import java.net.HttpURLConnection; //導入方法依賴的package包/類
private void manageError(Exception e, HttpURLConnection urlConnection) {
    if (SimpleNetworkUtils.isNetworkAvailable(context)) {
        if (urlConnection != null) {
            try {
                code = urlConnection.getResponseCode();
                if (urlConnection.getErrorStream() != null) {
                    InputStream is = urlConnection.getErrorStream();
                    StringBuilder buffer = new StringBuilder();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                    String line;
                    while ((line = reader.readLine()) != null) {
                        buffer.append(line).append("\n");
                    }
                    message = buffer.toString();
                } else {
                    message = urlConnection.getResponseMessage();
                }
                error = urlConnection.getErrorStream().toString();
                Log.e(LOG_TAG, "Error: " + message + ", code: " + code);
            } catch (IOException e1) {
                e1.printStackTrace();
                Log.e(LOG_TAG, "Error: " + e1.getMessage());
            }
        } else {
            code = 105;
            error = e.getMessage();
            message = "Error: No internet connection";
            Log.e(LOG_TAG, "code: " + code + ", " + message);
        }
    } else {
        code = 105;
        error = e.getMessage();
        message = "Error: No internet connection";
        Log.e(LOG_TAG, "code: " + code + ", " + message);
    }
}
 
開發者ID:ahmed-adel-said,項目名稱:SimpleNetworkLibrary,代碼行數:37,代碼來源:NetworkRequestAsyncTask.java

示例9: entityFromConnection

import java.net.HttpURLConnection; //導入方法依賴的package包/類
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    InputStream inputStream;
    BasicHttpEntity entity = new BasicHttpEntity();
    try {
        inputStream = connection.getInputStream();
    } catch (IOException e) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength((long) connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}
 
開發者ID:JackChan1999,項目名稱:boohee_v5.6,代碼行數:15,代碼來源:OkHttpStack.java

示例10: getResponseStream

import java.net.HttpURLConnection; //導入方法依賴的package包/類
private InputStream getResponseStream(HttpURLConnection connection) {
    InputStream input = null;
    try {
        input = connection.getInputStream();
    } catch (IOException ioEx) {
        input = connection.getErrorStream();
    }
    return input;
}
 
開發者ID:nongfenqi,項目名稱:dingtalk-incoming-webhoot-plugin,代碼行數:10,代碼來源:DingtalkNotificationPlugin.java

示例11: getErrorStream

import java.net.HttpURLConnection; //導入方法依賴的package包/類
private InputStream getErrorStream(HttpURLConnection connection)
		throws IOException {
	if ("gzip".equalsIgnoreCase(connection.getContentEncoding())) {
		return new GZIPInputStream(connection.getErrorStream());
	}

	if ("deflate".equals(connection.getContentEncoding())) {
		return new InflaterInputStream(connection.getErrorStream());
	}
	return connection.getErrorStream();
}
 
開發者ID:RayTW,項目名稱:8ComicSDK-JAVA,代碼行數:12,代碼來源:EasyHttp.java

示例12: entityFromConnection

import java.net.HttpURLConnection; //導入方法依賴的package包/類
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;
}
 
開發者ID:DLKanth,項目名稱:Stetho-Volley,代碼行數:15,代碼來源:StethoVolleyStack.java

示例13: open

import java.net.HttpURLConnection; //導入方法依賴的package包/類
private static int open(String str) {
    InputStream inputStream = null;
    try {
        int i;
        HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(str).openConnection();
        httpURLConnection.setRequestMethod("GET");
        httpURLConnection.setConnectTimeout(5000);
        httpURLConnection.setReadTimeout(5000);
        httpURLConnection.setUseCaches(false);
        if (200 == httpURLConnection.getResponseCode()) {
            inputStream = httpURLConnection.getInputStream();
            StreamToString(inputStream);
            i = 1;
        } else {
            inputStream = httpURLConnection.getErrorStream();
            StreamToString(inputStream);
            i = 0;
        }
        String.valueOf(i);
        if (inputStream == null) {
            return i;
        }
        try {
            inputStream.close();
            return i;
        } catch (IOException e) {
            e.printStackTrace();
            return i;
        }
    } catch (Exception e2) {
        e2.printStackTrace();
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e3) {
                e3.printStackTrace();
            }
        }
        return 0;
    } catch (Throwable th) {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e4) {
                e4.printStackTrace();
            }
        }
    }
}
 
開發者ID:JackChan1999,項目名稱:letv,代碼行數:50,代碼來源:a.java

示例14: GetStreamURI

import java.net.HttpURLConnection; //導入方法依賴的package包/類
static String GetStreamURI(String username, String quality) {
    try {
        //FIXME: HTTP is bad!
        URL url = new URL("http://usher.twitch.tv/api/channel/hls/" + URLEncoder.encode(username, "UTF-8") + ".m3u8?player=twitchweb&token=" +
                URLEncoder.encode(GetAccessToken(username), "UTF-8") + "&sig=" + URLEncoder.encode(GetSignature(username), "UTF-8") + "&allow_source=true&allow_audio_only=true");

        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestProperty("Client-ID", "zdol60x6sxbx0phk60ci6mki79qhrf");
        urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36");
        urlConnection.setUseCaches(false);

        InputStream inputStream;
        int status = urlConnection.getResponseCode();

        if (status != HttpURLConnection.HTTP_OK)
            inputStream = urlConnection.getErrorStream();
        else
            inputStream = urlConnection.getInputStream();

        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));

            boolean nextURL = false;

            String line = br.readLine();
            while(line != null)
            {
                if(nextURL) {
                    return line;
                } else {
                    final Pattern pattern = Pattern.compile("^#EXT-X-STREAM-INF:.*VIDEO=\"(?<value>[^\"]*)");

                    Matcher matcher = pattern.matcher(line);
                    if (matcher.find()) {
                        String streamQuality = matcher.group(1);

                        //this is it!!
                        if (streamQuality.equals(quality)) {
                            nextURL = true;
                        }
                    }
                }

                line = br.readLine();
            }

            br.close();
        } finally {
            urlConnection.disconnect();
        }
    } catch(Exception e) {
        e.printStackTrace();
    }

    return "undefined";
}
 
開發者ID:invghost,項目名稱:NeoStream,代碼行數:57,代碼來源:TwitchAPI.java

示例15: getResponse

import java.net.HttpURLConnection; //導入方法依賴的package包/類
public static HttpResponse getResponse(HttpRequest request) throws IOException {
	OutputStream out= null;
	InputStream content = null;
	HttpResponse response = null;
	HttpURLConnection httpConn = request.getHttpConnection();

	try {
		httpConn.connect();
		if (null != request.getContent()) {
			out = httpConn.getOutputStream();
			out.write(request.getContent());
		}
		content = httpConn.getInputStream();
		response = new HttpResponse(httpConn.getURL().toString());
		pasrseHttpConn(response, httpConn, content);
		return response;
	} catch (IOException e) {
		content = httpConn.getErrorStream();
		response = new HttpResponse(httpConn.getURL().toString());
		pasrseHttpConn(response, httpConn, content);
		return response;
	} finally {
		if (content != null) 
			content.close();
           httpConn.disconnect();
       }
}
 
開發者ID:readen,項目名稱:Relay,代碼行數:28,代碼來源:HttpResponse.java


注:本文中的java.net.HttpURLConnection.getErrorStream方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。