作者:口十又欠又欠 | 来源:互联网 | 2023-05-19 16:40
***发送HttpClient**publicclassHttpClientTest{publicstaticvoidmain(String[]args)throwsI
/**
* 发送HttpClient
*
*/
public class HttpClientTest {
public static void main(String[] args) throws IOException {
delete();
}
public static void query() throws IOException {
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(
"http://localhost:7001/pa18shoplife/lifegame/ItemareaBag/query.do3");
Map map = new HashMap();
List list = new ArrayList();
list.add("41");
list.add("42");
map.put("ids", list);
map.put("pageNumber", "1");
map.put("pageSize", "5000");
String str = Utils.convertToString4Obj(map);
System.out.println(str);
respCallback(client, post, str);
}
public static void saveorupdate() throws IOException,
UnsupportedEncodingException, ClientProtocolException {
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(
"http://localhost:7001/pa18shoplife/lifegame/ItemareaBag/saveorupdate.do3");
List list = new ArrayList();
LifeGameItemareaBagDTO dto = new LifeGameItemareaBagDTO();
dto.setId(41);
dto.setRoleId(3);
dto.setGridbag("11111");
dto.setGridnum(3);
list.add(dto);
dto = new LifeGameItemareaBagDTO();
dto.setId(42);
dto.setRoleId(4);
dto.setGridbag("222222");
dto.setGridnum(3);
list.add(dto);
String jsOnStr= JSON.json(list);
System.out.println(jsonStr);
respCallback(client, post, jsonStr);
}
public static void delete() throws IOException,
UnsupportedEncodingException, ClientProtocolException {
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(
"http://localhost:7001/pa18shoplife/lifegame/ItemareaBag/delete.do3");
List
/**
* 物品区域 --> 背包
*
*/
public class LifeGameItemareaBagDTO {
private long id;
private long roleId;
private String gridbag;
private int gridnum;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getRoleId() {
return roleId;
}
public void setRoleId(long roleId) {
this.roleId = roleId;
}
public String getGridbag() {
return gridbag;
}
public void setGridbag(String gridbag) {
this.gridbag = gridbag;
}
public int getGridnum() {
return gridnum;
}
public void setGridnum(int gridnum) {
this.gridnum = gridnum;
}
}
/**
* 专用工具类
*
*/
public class Utils {
private static final Logger logger = LoggerFactory.getLogger(Utils.class);
/**
* @param obj
* @return 返回JSON字符串
*/
public static String convertToString4Obj(T obj){
try {
return JSON.json(obj);
} catch (IOException e) {
e.printStackTrace();
}
return "{\"resultCode\":-500,\"msg\":\"不能把Obj变为JSON字符串.\"}";
}
/**
* @param clazz
* @param req
* @return
* @throws Exception
*
* 根据请求的request 的 body 返回 clazz的list形式
*/
public static List convertToListObject(Class clazz,
HttpServletRequest req) throws Exception{
return convertToListObject(clazz, toString(req));
}
/**
* @param clazz
* @param jsonStr
* @return
*
* 根据字符串返回clazz的list形式
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static List convertToListObject(Class clazz, String jsonStr)
throws Exception{
List retVal = new ArrayList();
if(StringUtils.isBlank(jsonStr)){
return retVal;
}
try {
Object obj = JSON.parse(jsonStr);
if (obj instanceof JSONArray) {
List list = JSON.parse(jsonStr, List.class);
if(clazz == Map.class){
return (List) list;
}
for (Map map : list) {
T t = clazz.newInstance();
BeanUtils.copyProperties(t, map);
retVal.add(t);
}
} else if (obj instanceof JSONObject) {
Map map = JSON.parse(jsonStr, Map.class);
if(clazz == Map.class){
retVal.add((T) map);
return retVal;
}
T t = clazz.newInstance();
BeanUtils.copyProperties(t, map);
retVal.add(t);
}
} catch (ParseException e) {
logger.info("parse JSON error.......",e);
throw e;
} catch (InstantiationException e) {
logger.info("instance clazz[{}] error.......",clazz,e);
throw e;
} catch (IllegalAccessException e) {
logger.info("IllegalAccessException........",e);
throw e;
} catch (InvocationTargetException e) {
logger.info("InvocationTargetException........",e);
throw e;
}
return retVal;
}
/**
* @param req
* @return
* @throws IOException
*
* 返回req的body字符串
*/
public static String toString(HttpServletRequest req) throws IOException {
ByteArrayOutputStream writer = new ByteArrayOutputStream();
InputStream ins = null;
String retVal = null;
try {
ins = req.getInputStream();
if (ins != null) {
IOUtils.copy(ins, writer);
retVal = new String(writer.toByteArray(),"utf-8");
}
} finally {
IOUtils.closeQuietly(ins);
IOUtils.closeQuietly(writer);
}
logger.info("request body is [{}]",retVal);
return retVal;
}
/**
* @param req
* @return
* @throws IOException
*
*/
@SuppressWarnings("rawtypes")
public static Map convertToMap(HttpServletRequest req) throws Exception {
String jsOnStr= toString(req);
if(StringUtils.isNotBlank(jsonStr)){
return JSON.parse(jsonStr, Map.class);
}
return null;
}
}
相关jar包:
commons-beanutils.jar
commons-collections-3.1.jar
commons-io-1.4.jar
commons-lang.jar
commons-logging.jar
dubbo-2.4.9.1.jar
httpclient-4.3.6.jar
httpcore-4.3.3.jar
javaee.jar
javassist-3.16.1-GA.jar
log4j-1.2.15.jar
slf4j-api-1.7.2.jar
slf4j-log4j12-1.7.2.jar