tip:加密部分暂时先注释掉
1 package com.zqc.share.manager.framework; 2 3 import java.net.HttpURLConnection; 4 import java.net.URL; 5 6 7 import java.io.ByteArrayOutputStream; 8 import java.io.DataOutputStream; 9 import java.io.InputStream; 10 import java.io.UnsupportedEncodingException; 11 12 import org.codehaus.jackson.map.ObjectMapper; 13 14 import com.zqc.share.manager.model.ResponseBody; 15 16 17 18 19 public class HttpProxy { 20 21 //根据对象获取字符json串 22 public static String getObjectString(Object object){ 23 //objectMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, Boolean.TRUE); 24 String ObjectString = null; 25 try { 26 ObjectMapper objectMapper = new ObjectMapper(); 27 ObjectString = objectMapper.writeValueAsString(object); 28 } catch (Exception e) { 29 e.printStackTrace(); 30 } 31 /*if(ObjectString != null){ 32 byte[] bytes = AESManager.decrypt(AESManager.parseHexStr2Byte(ObjectString), AESManager.password); 33 try { 34 ObjectString = new String(bytes,"utf-8"); 35 } catch (UnsupportedEncodingException e) { 36 // TODO Auto-generated catch block 37 e.printStackTrace(); 38 } 39 }*/ 40 return ObjectString; 41 42 } 43 44 //由json转化为对象 45 public static Object getObject(String string){ 46 ResponseBody responseBody = new ResponseBody(); 47 try { 48 ObjectMapper objectMapper = new ObjectMapper(); 49 responseBody = objectMapper.readValue(string, ResponseBody.class); 50 } catch (Exception e) { 51 // TODO Auto-generated catch block 52 e.printStackTrace(); 53 } 54 /*try { 55 responseBody.setResponseContext(new String(AESManager.decrypt(AESManager.parseHexStr2Byte(responseBody.getResponseContext()), AESManager.password),"utf-8")); 56 } catch (UnsupportedEncodingException e) { 57 // TODO Auto-generated catch block 58 e.printStackTrace(); 59 }*/ 60 return responseBody; 61 } 62 63 public static Object getObject(String string,Class clazz){ 64 Object object = null; 65 try { 66 ObjectMapper objectMapper = new ObjectMapper(); 67 object = objectMapper.readValue(string, clazz); 68 } catch (Exception e) { 69 e.printStackTrace(); 70 } 71 return object; 72 } 73 74 //发送post请求 75 public static String post(String url, Object object,String charset){ 76 try { 77 String content; 78 ObjectMapper objectMapper = new ObjectMapper(); 79 content = objectMapper.writeValueAsString(object); 80 URL console = new URL(url); 81 HttpURLConnection conn = (HttpURLConnection) console.openConnection(); 82 conn.setDoOutput(true); 83 conn.setDoInput(true); 84 conn.setRequestMethod("POST"); 85 conn.setUseCaches(false); 86 conn.setInstanceFollowRedirects(true); 87 conn.setRequestProperty("Content-Type","application/json"); 88 conn.connect(); 89 DataOutputStream out = new DataOutputStream(conn.getOutputStream()); 90 out.write(content.getBytes(charset)); 91 out.flush(); 92 out.close(); 93 InputStream is = conn.getInputStream(); 94 if (is != null) { 95 ByteArrayOutputStream outStream = new ByteArrayOutputStream(); 96 byte[] buffer = new byte[1024]; 97 int len = 0; 98 while ((len = is.read(buffer)) != -1) { 99 outStream.write(buffer, 0, len); 100 } 101 is.close(); 102 return outStream.toString("utf-8"); 103 } 104 return new String(); 105 } catch (Exception e) { 106 e.printStackTrace(); 107 return new String(); 108 } 109 } 110 }