国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 山西省| 济源市| 南汇区| 汾西县| 吉林省| 寿阳县| 华池县| 桃园县| 山西省| 七台河市| 贵阳市| 姚安县| 达孜县| 台北市| 综艺| 大悟县| 靖宇县| 蕲春县| 高碑店市| 边坝县| 巨野县| 河北省| 区。| 定襄县| 五常市| 利川市| 山阴县| 临泉县| 阳东县| 哈巴河县| 秦安县| 利辛县| 泰和县| 虎林市| 苍溪县| 金门县| 望城县| 哈巴河县| 东阳市| 卢氏县| 习水县|