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


Java HttpURLConnection.getContentEncoding方法代碼示例

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


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

示例1: 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) {
        String contentEncoding = conn.getContentEncoding();
        if (CONTENT_ENCODING_GZIP.equalsIgnoreCase(contentEncoding)) {
            return getStreamAsString(new GZIPInputStream(conn.getInputStream()), charset);
        } else {
            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:warlock-china,項目名稱:azeroth,代碼行數:20,代碼來源:HttpUtils.java

示例2: makeContent

import java.net.HttpURLConnection; //導入方法依賴的package包/類
/**
 * 得到響應對象
 * 
 * @param urlConnection
 * @return 響應對象
 * @throws IOException
 */
private HttpRespons makeContent(String urlString, HttpURLConnection urlConnection) throws IOException {
	HttpRespons httpResponser = new HttpRespons();
	try {
		InputStream in = urlConnection.getInputStream();
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in,
				Charset.forName(this.defaultContentEncoding)));
		httpResponser.contentCollection = new Vector<String>();
		StringBuffer temp = new StringBuffer();
		String line = bufferedReader.readLine();
		while (line != null) {
			httpResponser.contentCollection.add(line);
			temp.append(line).append("");
			line = bufferedReader.readLine();
		}
		bufferedReader.close();

		String ecod = urlConnection.getContentEncoding();
		if (ecod == null)
			ecod = this.defaultContentEncoding;

		httpResponser.urlString = urlString;

		httpResponser.defaultPort = urlConnection.getURL().getDefaultPort();
		httpResponser.file = urlConnection.getURL().getFile();
		httpResponser.host = urlConnection.getURL().getHost();
		httpResponser.path = urlConnection.getURL().getPath();
		httpResponser.port = urlConnection.getURL().getPort();
		httpResponser.protocol = urlConnection.getURL().getProtocol();
		httpResponser.query = urlConnection.getURL().getQuery();
		httpResponser.ref = urlConnection.getURL().getRef();
		httpResponser.userInfo = urlConnection.getURL().getUserInfo();

		httpResponser.content = temp.toString();
		httpResponser.contentEncoding = ecod;
		httpResponser.code = urlConnection.getResponseCode();
		httpResponser.message = urlConnection.getResponseMessage();
		httpResponser.contentType = urlConnection.getContentType();
		httpResponser.method = urlConnection.getRequestMethod();
		httpResponser.connectTimeout = urlConnection.getConnectTimeout();
		httpResponser.readTimeout = urlConnection.getReadTimeout();

		return httpResponser;
	} catch (IOException e) {
		throw e;
	} finally {
		if (urlConnection != null)
			urlConnection.disconnect();
	}
}
 
開發者ID:butter-fly,項目名稱:belling-spring-rabbitmq,代碼行數:57,代碼來源:HttpRequester.java

示例3: call

import java.net.HttpURLConnection; //導入方法依賴的package包/類
public Response call()
{
	try
	{
		HttpURLConnection con = (HttpURLConnection) URLUtils.newURL(url, url.getPath() + generateParamaters())
			.openConnection();
		con.setConnectTimeout(10000);
		con.setRequestProperty("User-Agent", "OAIHarvester/2.0");
		String enc = con.getContentEncoding();
		if( enc == null )
		{
			enc = "UTF-8";
		}
		return (Response) xstream.fromXML(new UnicodeReader(con.getInputStream(), enc));
	}
	catch( IOException e )
	{
		throw new RuntimeException(e);
	}
}
 
開發者ID:equella,項目名稱:Equella,代碼行數:21,代碼來源:Verb.java

示例4: 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

示例5: getResponseContent

import java.net.HttpURLConnection; //導入方法依賴的package包/類
/**
 * This method reads from a stream based on the passed connection
 * @param connection the connection to read from
 * @return the contents of the stream
 * @throws IOException if it cannot read from the http connection
 */
private static String getResponseContent(HttpURLConnection connection) throws IOException {
  // Use the content encoding to convert bytes to characters.
  String encoding = connection.getContentEncoding();
  if (encoding == null) {
    encoding = "UTF-8";
  }
  InputStreamReader reader = new InputStreamReader(connection.getInputStream(), encoding);
  try {
    int contentLength = connection.getContentLength();
    StringBuilder sb = (contentLength != -1)
        ? new StringBuilder(contentLength)
        : new StringBuilder();
    char[] buf = new char[1024];
    int read;
    while ((read = reader.read(buf)) != -1) {
      sb.append(buf, 0, read);
    }
    return sb.toString();
  } finally {
    reader.close();
  }
}
 
開發者ID:mit-cml,項目名稱:appinventor-extensions,代碼行數:29,代碼來源:YandexTranslate.java

示例6: getResponseContent

import java.net.HttpURLConnection; //導入方法依賴的package包/類
private static String getResponseContent(HttpURLConnection connection) throws IOException {
  // Use the content encoding to convert bytes to characters.
  String encoding = connection.getContentEncoding();
  if (encoding == null) {
    encoding = "UTF-8";
  }
  InputStreamReader reader = new InputStreamReader(getConnectionStream(connection), encoding);
  try {
    int contentLength = connection.getContentLength();
    StringBuilder sb = (contentLength != -1)
        ? new StringBuilder(contentLength)
        : new StringBuilder();
    char[] buf = new char[1024];
    int read;
    while ((read = reader.read(buf)) != -1) {
      sb.append(buf, 0, read);
    }
    return sb.toString();
  } finally {
    reader.close();
  }
}
 
開發者ID:mit-cml,項目名稱:appinventor-extensions,代碼行數:23,代碼來源:Web.java

示例7: postGetJson

import java.net.HttpURLConnection; //導入方法依賴的package包/類
public static String postGetJson(String url, String content) {
        try {
            URL mUrl = new URL(url);
            HttpURLConnection mHttpURLConnection = (HttpURLConnection) mUrl.openConnection();
            //設置鏈接超時時間
            mHttpURLConnection.setConnectTimeout(15000);
            //設置讀取超時時間
            mHttpURLConnection.setReadTimeout(15000);
            //設置請求參數
            mHttpURLConnection.setRequestMethod("POST");
            //添加Header
            mHttpURLConnection.setRequestProperty("Connection", "Keep-Alive");
            //接收輸入流
            mHttpURLConnection.setDoInput(true);
            //傳遞參數時需要開啟
            mHttpURLConnection.setDoOutput(true);
            //Post方式不能緩存,需手動設置為false
            mHttpURLConnection.setUseCaches(false);

            mHttpURLConnection.connect();

            DataOutputStream dos = new DataOutputStream(mHttpURLConnection.getOutputStream());

            String postContent = content;

            dos.write(postContent.getBytes());
            dos.flush();
            // 執行完dos.close()後,POST請求結束
            dos.close();
            // 獲取代碼返回值
            int respondCode = mHttpURLConnection.getResponseCode();
            Log.d("respondCode","respondCode="+respondCode );
            // 獲取返回內容類型
            String type = mHttpURLConnection.getContentType();
            Log.d("type", "type="+type);
            // 獲取返回內容的字符編碼
            String encoding = mHttpURLConnection.getContentEncoding();
            Log.d("encoding", "encoding="+encoding);
            // 獲取返回內容長度,單位字節
            int length = mHttpURLConnection.getContentLength();
            Log.d("length", "length=" + length);
//            // 獲取頭信息的Key
//            String key = mHttpURLConnection.getHeaderField(idx);
//            Log.d("key", "key="+key);
            // 獲取完整的頭信息Map
            Map<String, List<String>> map = mHttpURLConnection.getHeaderFields();
            if (respondCode == 200) {
                // 獲取響應的輸入流對象
                InputStream is = mHttpURLConnection.getInputStream();
                // 創建字節輸出流對象
                ByteArrayOutputStream message = new ByteArrayOutputStream();
                // 定義讀取的長度
                int len = 0;
                // 定義緩衝區
                byte buffer[] = new byte[1024];
                // 按照緩衝區的大小,循環讀取
                while ((len = is.read(buffer)) != -1) {
                    // 根據讀取的長度寫入到os對象中
                    message.write(buffer, 0, len);
                }
                // 釋放資源
                is.close();
                message.close();
                // 返回字符串
                String msg = new String(message.toByteArray());
                Log.d("Common", msg);
                return msg;
            }
            return "fail";
        }catch(Exception e){
            return "error";
        }
    }
 
開發者ID:Luodian,項目名稱:Shared-Route,代碼行數:74,代碼來源:CommonHttp.java


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