配置如下:
application-context.xml
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-autowire="byName">
context:annotation-config/
dispatcherContext-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 使Controller中的@RequestMapping 生效-->
<!-- 解决ajax调用中文乱码问题 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- <property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />-->
<property name="suffix" value=".jsp" />
</bean>
被调用的类
package com.webservice.common;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.base.core.log4j.ILog4jManager;
public class LocalHttpClient {
private static HttpParams httpParams;
private static PoolingClientConnectionManager connectionManager;
/**
* 最大连接数
*/
public final static int MAX_TOTAL_CONNECTIONS = 100;
/**
* 每个路由最大连接数
*/
public final static int MAX_ROUTE_CONNECTIONS = 20;
/**
* 连接超时时间
*/
public final static int CONNECT_TIMEOUT = 10000;
/**
* 读取超时时间
*/
public final static int READ_TIMEOUT = 10000;
@Autowired
private ILog4jManager log4jManager;
static {
// 设置组件参数, HTTP协议的版本,1.1/1.0/0.9
httpParams = new BasicHttpParams();
HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_0);
HttpProtocolParams.setUserAgent(httpParams, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)");
HttpProtocolParams.setUseExpectContinue(httpParams, true);
//设置连接超时时间
httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECT_TIMEOUT);
httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, READ_TIMEOUT);
//设置访问协议
SchemeRegistry schreg = new SchemeRegistry();
schreg.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
schreg.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
//多连接的线程安全的管理器
connectionManager = new PoolingClientConnectionManager(schreg);
connectionManager.setDefaultMaxPerRoute(MAX_ROUTE_CONNECTIONS); //每个主机的最大并行链接数
connectionManager.setMaxTotal(MAX_TOTAL_CONNECTIONS); //客户端总并行链接最大数
}
/**
* 获取httpClient
*
* @return
*/
public static HttpClient getHttpClient() {
return new DefaultHttpClient(connectionManager, httpParams);
}
public String postMethod(String url,List<NameValuePair> list) throws Exception{
HttpPost httppost = new HttpPost(url);
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "utf-8");
entity.toString();
httppost.setEntity(entity);
HttpClient client = LocalHttpClient.getHttpClient();
HttpResponse httpResponse = client.execute(httppost);
String xmlContent=EntityUtils.toString(httpResponse.getEntity());
String tex = new String(xmlContent.getBytes("ISO-8859-1"),"UTF-8");
//这个地方异常
log4jManager.infoCustomLog(getClass().getName(),"postMethod", tex);****
return tex;
}
}
单元测试类
package com.base.junit4support;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.base.core.Constants;
import com.base.core.log4j.ILog4jManager;
import com.webservice.common.LocalHttpClient;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationConfiguration-context.xml" })
public class AbstractSpringJUnit4Support extends AbstractJUnit4SpringContextTests {
@Autowired
protected ILog4jManager log4jManager;;
@Test
public void say(){
System.out.println("000000");
//这个地方正常
log4jManager.debugCustomLog(getClass().getName(),"say", "正确");
LocalHttpClient localHttpClient = new LocalHttpClient();
List<NameValuePair> list = new ArrayList<NameValuePair>();
try {
String xml = localHttpClient.postMethod(Constants.MOBILE_RECHARGE_URL, list);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("wwww");
}
求解。。。。