tip:加密部分暂时先注释掉
package com.zqc.share.manager.framework; import java.net.HttpURLConnection;
import java.net.URL; import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException; import org.codehaus.jackson.map.ObjectMapper; import com.zqc.share.manager.model.ResponseBody; public class HttpProxy { //根据对象获取字符json串
public static String getObjectString(Object object){
//objectMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, Boolean.TRUE);
String ObjectString = null;
try {
ObjectMapper objectMapper = new ObjectMapper();
ObjectString = objectMapper.writeValueAsString(object);
} catch (Exception e) {
e.printStackTrace();
}
/*if(ObjectString != null){
byte[] bytes = AESManager.decrypt(AESManager.parseHexStr2Byte(ObjectString), AESManager.password);
try {
ObjectString = new String(bytes,"utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}*/
return ObjectString; } //由json转化为对象
public static Object getObject(String string){
ResponseBody responseBody = new ResponseBody();
try {
ObjectMapper objectMapper = new ObjectMapper();
responseBody = objectMapper.readValue(string, ResponseBody.class);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*try {
responseBody.setResponseContext(new String(AESManager.decrypt(AESManager.parseHexStr2Byte(responseBody.getResponseContext()), AESManager.password),"utf-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
return responseBody;
} public static Object getObject(String string,Class clazz){
Object object = null;
try {
ObjectMapper objectMapper = new ObjectMapper();
object = objectMapper.readValue(string, clazz);
} catch (Exception e) {
e.printStackTrace();
}
return object;
} //发送post请求
public static String post(String url, Object object,String charset){
try {
String content;
ObjectMapper objectMapper = new ObjectMapper();
content = objectMapper.writeValueAsString(object);
URL console = new URL(url);
HttpURLConnection conn = (HttpURLConnection) console.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.setUseCaches(false);
conn.setInstanceFollowRedirects(true);
conn.setRequestProperty("Content-Type","application/json");
conn.connect();
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.write(content.getBytes(charset));
out.flush();
out.close();
InputStream is = conn.getInputStream();
if (is != null) {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
is.close();
return outStream.toString("utf-8");
}
return new String();
} catch (Exception e) {
e.printStackTrace();
return new String();
}
}
}