关于字符编码格式

1.服务器可以设置编码格式

2.nginx也可以设置编码格式

3.httpCilent工具(jar包),会先获取response中返回的Content-Type中包含的编码格式charset,如果没取到,则取httpClient工具中的default charset 为ISO-8859-1,httpclient中有给各种协议中传输数据的编码格式默认设置。如需要改变,需要在调用的时候去设置

    params.setHttpElementCharset("US-ASCII");//创建httpHeader的字符集默认值
    params.setContentCharset("ISO-8859-1");//创建contentBody的字符集默认值
  public static final String CREDENTIAL_CHARSET = "http.protocol.credential-charset";
  public static final String HTTP_ELEMENT_CHARSET = "http.protocol.element-charset";
  public static final String HTTP_URI_CHARSET = "http.protocol.uri-charset";//http.protocol.uri-charset这个值get方法先取,取不到httpclient中设置为utf-8,初始化的时候是没值的,就会取到utf-8
  public static final String HTTP_CONTENT_CHARSET = "http.protocol.content-charset";


4.String的默认编码格式和系统有关系,String 的处理是通过getByte("charset")来处理的,会根据括号里的字符格式来转换成对应的字节码,如果String当前默认编码是utf-8格式的,getbyte中用iso-8859-1会出转换成乱码。

//			String responseStr = resBuffer.toString();
			//			byte[] responseBody = responseStr.getBytes(getMethod.getResponseCharSet());
	public static String httpVisit(String url, String charset) {
		// 构造HttpClient的实例
		HttpClient httpClient = new HttpClient();
		httpClient.setConnectionTimeout(1500);
		//		httpClient.getParams().setParameter("http.protocol.content-charset", charset);
		// 创建GET方法的实例
		GetMethod getMethod = new GetMethod(url);
		getMethod.addRequestHeader("Connection", "close");
		try {
			// 执行getMethod
			int statusCode = httpClient.executeMethod(getMethod);
			if (statusCode != HttpStatus.SC_OK) {
				System.err.println("Method failed: " + getMethod.getStatusLine());
			}
			// 返回响应消息
			InputStream resStream = getMethod.getResponseBodyAsStream();
			long contentLength = getMethod.getResponseContentLength();
			if (contentLength > 2147483647L) {
				throw new IOException("Content too large to be buffered: " + contentLength + " bytes");
			}
			int limit = getMethod.getParams().getIntParameter("http.method.response.buffer.warnlimit", 1048576);
			if ((contentLength == -1L) || (contentLength > limit)) {
				System.out.println("Too long");
			}
			ByteArrayOutputStream outstream = new ByteArrayOutputStream(contentLength > 0L ? (int) contentLength : 4096);
			byte[] buffer = new byte[4096];
			int len;
			while ((len = resStream.read(buffer)) > 0) {
				outstream.write(buffer, 0, len);
			}
			outstream.close();
			String content = new String(outstream.toByteArray(), charset);
			//			InputStream resStream = getMethod.getResponseBodyAsStream();
			//			BufferedReader br = new BufferedReader(new InputStreamReader(resStream));
			//			StringBuffer resBuffer = new StringBuffer();
			//			String resTemp = "";
			//			while ((resTemp = br.readLine()) != null) {
			//				resBuffer.append(resTemp);
			//			}
			//			String responseStr = resBuffer.toString();
			//			byte[] responseBody = responseStr.getBytes(getMethod.getResponseCharSet());
			//			String content = new String(responseBody, charset);

			//			byte[] responseBody1 = getMethod.getResponseBodyAsString().getBytes(getMethod.getResponseCharSet());
			//			String content1 = new String(responseBody1, charset);
			//			return content1;
			return content;
		} catch (HttpException e) {// 发生致命的异常,可能是协议不对或者返回的内容有问题
			LogHelper.exceptionLog(e);
			return "-1";
		} catch (IOException e) {
			// 发生网络异常
			LogHelper.exceptionLog(e);
			return "-1";
		} finally {
			try {
				// 释放连接
				getMethod.releaseConnection();
				httpClient.getHttpConnectionManager().closeIdleConnections(1);
			} catch (Exception e) {
				LogHelper.exceptionLog(e);
			}
		}
	}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值