使用jdk自带的HttpURLConnection发送json请求
2018-03-19 11:47
471 查看
下面是发送代码的方法,目前使用的是jdk1.8测试的
/** * 发送http POST请求 * * @param * @return 远程响应结果 */ public static String sendPost(String u, String json) { StringBuffer sbf = new StringBuffer(); try { URL url = new URL(u); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setUseCaches(false); connection.setInstanceFollowRedirects(true); connection.addRequestProperty("role", "Admin"); connection.addRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); connection.connect(); DataOutputStream out = new DataOutputStream(connection.getOutputStream()); if (!"".equals(json)) { out.writeBytes(json); } out.flush(); out.close(); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String lines; while ((lines = reader.readLine()) != null) { lines = new String(lines.getBytes(), "utf-8"); sbf.append(lines); } System.out.println(sbf); reader.close(); // 断开连接 connection.disconnect(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generate ae0c d catch block e.printStackTrace(); } return sbf.toString(); } public static String sendPostjason(String url, String xml) { // File input = new File("test.xml");//如果是xml文件,可以这样写 PostMethod post = new PostMethod(url);// 请求地址 // 设置请求的内容直接从文件中读取 // post.setRequestBody( new FileInputStream(input)); // if (input.length() < Integer.MAX_VALUE) // post.setRequestContentLength(input.length()); // else // post.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED); post.setRequestBody(xml);// 这里添加xml字符串 // 指定请求内容的类型 post.setRequestHeader("Content-type", "application/json; charset=UTF-8"); post.setRequestHeader("role", "Admin"); HttpClient httpclient = new HttpClient();// 创建 HttpClient 的实例 int result; try { result = httpclient.executeMethod(post); System.out.println("Response status code: " + result);// 返回200为成功 System.out.println("Response body: "); return post.getResponseBodyAsString();// 返回的内容 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public static String sendGet(String u, String json) { StringBuffer sbf = new StringBuffer(); try { URL url = new URL(u); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.addRequestProperty("role", "Admin"); connection.addRequestProperty("Content-Type", "application/json"); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String lines; while ((lines = reader.readLine()) != null) { lines = new String(lines.getBytes(), "utf-8"); sbf.append(lines); } System.out.println(sbf); reader.close(); // 断开连接 connection.disconnect(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return sbf.toString(); }
相关文章推荐
- 使用JDK自带HttpsURLConnection发送HTTPS请求
- Http学习之使用HttpURLConnection发送post和get请求
- 使用HttpURLConnection开发自动发送Get、Post请求并获取响应
- IT忍者神龟之使用HttpURLConnection向服务器发送post和get请求
- Android使用HttpUrlConnection请求服务器发送数据详解
- Android使用HttpURLConnection发送Post请求到服务端
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post请求深入
- HttpURLConnection 发送post请求。并将结果以JSONObject对象返回的轮子
- Android使用HttpUrlConnection请求服务器发送数据详解
- URL和HttpURLConnection的使用(向服务器发送请求,然后返回struts版)
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求 .
- 使用HttpURLConnection发送XML请求数据
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求(1)
- 使用HttpURLConnection发送post和get请求
- Android/java 利用HttpUrlConnection 发送put请求,携带json参数
- Http学习之使用HttpURLConnection发送post请求深入