import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException;
import java.io.InputStreamReader; import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.URL;
import java.net.URLEncoder; import java.util.Map;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.web.bind.annotation.RequestMethod;
public class HttpUtils { public static connection.setUseCaches(false); connection.setInstanceFollowRedirects(true); connection.setRequestProperty(\"Accept\\"application/json\"); // 设置接收数据的格式 connection.setRequestProperty(\"Content-Type\\"application/json\"); // 设置发送数据的 connection.setRequestProperty(\"Accept-Charset\ connection.setRequestProperty(\"contentType\ connection.connect(); ObjectMapper oMapper = new ObjectMapper(); if (jsonObject != null && !\"\".equals(jsonObject)) { DataOutputStream out = new DataOutputStream( connection.getOutputStream()); out.write(oMapper.writeValueAsString(jsonObject).getBytes( \"utf-8\")); out.flush(); out.close(); } BufferedReader reader = new BufferedReader(new InputStreamReader( connection.getInputStream(), \"utf-8\")); String lines; StringBuffer sb = new StringBuffer(\"\"); while ((lines = reader.readLine()) != null) { sb.append(lines); } reader.close(); // 断开连接 connection.disconnect(); // System.out.println(sb); return oMapper.readValue(sb.toString(), valueType); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } public static String http_request(String requestUrl, Map 格式 数据的 for (Map.Entry } URL url = new URL(requestUrl); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setConnectTimeout(3000); connection.setReadTimeout(8000); if (RequestMethod.POST == requestMethod) { connection.setDoOutput(true); } connection.setDoInput(true); connection.setRequestMethod(requestMethod.name()); connection.setUseCaches(false); connection.setInstanceFollowRedirects(true); connection.setRequestProperty(\"Accept\\"application/json\"); // 设置接收数据的connection.setRequestProperty(\"Content-Type\\"application/json\"); // 设置发送connection.connect(); ObjectMapper oMapper = new ObjectMapper(); if (jsonObject != null && !\"\".equals(jsonObject)) { DataOutputStream out = new DataOutputStream( connection.getOutputStream()); out.write(oMapper.writeValueAsString(jsonObject).getBytes( \"utf-8\")); out.flush(); out.close(); } BufferedReader reader = new BufferedReader(new InputStreamReader( connection.getInputStream(), \"utf-8\")); String lines; StringBuffer sb = new StringBuffer(\"\"); while ((lines = reader.readLine()) != null) { sb.append(lines); } reader.close(); } } // 断开连接 connection.disconnect(); return sb.toString(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; 因篇幅问题不能全部显示,请点此查看更多更全内容