您的当前位置:首页java-http_get(post)

java-http_get(post)

2023-07-20 来源:乌哈旅游
package com.dajiesan.www.utils;

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 T http_request(String requestUrl, Map params, Object jsonObject, RequestMethod requestMethod, Class valueType) { try { // System.out.println(JsonUtils.writeValueAsString(jsonObject)); requestUrl += \"?\"; if (params != null && params.size() > 0) { for (Map.Entry itemEntry : params.entrySet()) { requestUrl += URLEncoder .encode(itemEntry.getKey(), \"utf-8\") + \"=\" + URLEncoder.encode( itemEntry.getValue().toString(), \"utf-8\") + \"&\"; } } URL url = new URL(requestUrl); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); // connection.setConnectTimeout(30000); // connection.setReadTimeout(30000); 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.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 params, Object jsonObject, RequestMethod requestMethod) { try { requestUrl += \"?\"; if (params != null && params.size() > 0) {

格式 数据的

for (Map.Entry itemEntry : params.entrySet()) { requestUrl += URLEncoder .encode(itemEntry.getKey(), \"utf-8\") + \"=\" + URLEncoder.encode( itemEntry.getValue().toString(), \"utf-8\") + \"&\"; }

}

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;

因篇幅问题不能全部显示,请点此查看更多更全内容