国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

HttpEntity的使用

2019-11-08 01:47:26
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

HttpEntity實(shí)體即可以使流也可以使字符串形式。

具體有什么用法看他的方法解釋:

[html] view plain copypackage com.scl.base;    import java.io.IOException;  import java.io.UnsupportedEncodingException;    import org.apache.http.HttpEntity;  import org.apache.http.ParseException;  import org.apache.http.entity.StringEntity;  import org.apache.http.util.EntityUtils;    public class HttpClientDemo06 {        /**       * @param args       */      public static void main(String[] args) {          try {              HttpEntity entity = new StringEntity("這一個(gè)字符串實(shí)體", "UTF-8");              //內(nèi)容類型              System.out.PRintln(entity.getContentType());              //內(nèi)容的編碼格式              System.out.println(entity.getContentEncoding());              //內(nèi)容的長(zhǎng)度              System.out.println(entity.getContentLength());              //把內(nèi)容轉(zhuǎn)成字符串              System.out.println(EntityUtils.toString(entity));              //內(nèi)容轉(zhuǎn)成字節(jié)數(shù)組              System.out.println(EntityUtils.toByteArray(entity).length);              //還有個(gè)直接獲得流              //entity.getContent();          } catch (UnsupportedEncodingException e) {              throw new RuntimeException(e);          } catch (ParseException e) {          } catch (IOException e) {          }                          }    }  對(duì)于實(shí)體的資源使用完之后要適當(dāng)?shù)幕厥召Y源,特別是對(duì)于流實(shí)體:例子代碼如下

[html] view plain copypublic static void test() throws IllegalStateException, IOException{          HttpResponse response = null;          HttpEntity entity = response.getEntity();                    if(entity!=null){                                 InputStream is = entity.getContent();                  try{                      //做一些操作                  }finally{                      //最后別忘了關(guān)閉應(yīng)該關(guān)閉的資源,適當(dāng)?shù)尼尫刨Y源                      if(is != null){                          is.close();                      }                      //這個(gè)方法也可以把底層的流給關(guān)閉了                      EntityUtils.consume(entity);                      //下面是這方法的源碼                      /*public static void consume(final HttpEntity entity) throws IOException {                          if (entity == null) {                              return;                          }                          if (entity.isStreaming()) {                              InputStream instream = entity.getContent();                              if (instream != null) {                                  instream.close();                              }                          }                      }*/                  }                                           }  
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 安丘市| 堆龙德庆县| 英山县| 武夷山市| 临沂市| 河间市| 凤山县| 顺平县| 新龙县| 白银市| 大新县| 桃园市| 喜德县| 东港市| 贡山| 江孜县| 鹿邑县| 繁昌县| 牙克石市| 万安县| 独山县| 沁水县| 年辖:市辖区| 林口县| 河西区| 水城县| 昭通市| 佛坪县| 芒康县| 廉江市| 大悟县| 茌平县| 岳西县| 宣武区| 新闻| 武安市| 镇沅| 庆元县| 台前县| 上思县| 昭觉县|