a我考网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 120|回复: 0

[其他] JAVA技巧:通过HttpClient获取响应内容字符集

[复制链接]
发表于 2012-8-4 12:28:23 | 显示全部楼层 |阅读模式
 /**   *正则匹配
  J4 s: @( R( B# d  * @param s
/ @: _; `: R8 @/ }/ v  * @param pattern
' K) d0 Q: {- @$ o, l7 j9 i# K  * @return
# _9 L# M  ~1 L7 p. {$ j0 p- [  */" _, a# G9 O+ ~0 s  p& W6 `% e
  public boolean matcher(String s, String pattern) { Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE + Pattern.UNICODE_CASE); Matcher matcher = p.matcher(s); if (matcher.find()) {; L, v* u, j1 k4 K/ |' g' H
  return true;
; u1 B* Y5 f1 f: b- R  } else {
9 b! g: C% {1 C( @  return false;
0 w. I: C6 D5 ]* V' V9 b  }6 t* i" S; {$ C2 g# b
  }
/ A$ M: t; J# Z8 O* l- g1 w' Y  /** *//**: I9 R" R7 l2 E8 r7 g- y2 B3 B
  *获取Response内容字符集( Q% l8 O) l  O) j3 i
  *. _  j  t* j, c
  * @param response
7 p7 K5 w  V8 b5 m) p! x  * @return3 o. D( U: J" C) \( Z  y2 h( i
  */( V$ u7 h1 E9 e6 y$ e) Q9 v
  public String getContentCharset(HttpResponse response) {
$ ~) L# k8 s0 u) v. B3 _  String charset = "ISO_8859-1";, j. a. T8 }+ r7 ~5 E
  Header header = response.getEntity().getContentType(); if (header != null) { String s = header.getValue(); if (matcher(s, "(charset)\s?=\s?(utf-?8)")) {2 h+ r& }$ ]( \% S. w
  charset = "utf-8";
5 ^6 m& q( p* k$ E/ ~# |  } else if (matcher(s, "(charset)\s?=\s?(gbk)")) {: K1 p# E& Q7 m$ y4 r$ d# }8 j' M
  charset = "gbk";& B+ y# o" D+ R: r" t- j+ U
  } else if (matcher(s, "(charset)\s?=\s?(gb2312)")) {
/ r8 z7 W( K! T) |0 z  charset = "gb2312";http://ks.com% F* l7 p0 I  j" u& R) T$ z
  }$ x0 m; m$ @5 b" f
  }
6 A' h, _1 P3 I/ r1 L5 p  |$ F! R  return charset;! S- i; J% ~7 w& c5 u7 q2 F
  }
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Woexam.Com ( 湘ICP备18023104号 )

GMT+8, 2024-5-4 09:06 , Processed in 0.174194 second(s), 21 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表