package com.sinosoft.ap.harmfullibrary.util;

/**
* 发送post请求
*/
import net.sf.json.JSONObject;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class HttpClientUtil {

public static void post(JSONObject json, String url) {
try {
CloseableHttpClient httpclient = HttpClients.createDefault();
System.out.println(json.toString());

HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");

// 解决中文乱码问题
StringEntity stringEntity = new StringEntity(json.toString(), "UTF-8");
stringEntity.setContentEncoding("UTF-8");

httpPost.setEntity(stringEntity);

System.out.println("Executing request " + httpPost.getRequestLine());

ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
@Override
public String handleResponse(final HttpResponse response)
throws ClientProtocolException, IOException {//
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {

HttpEntity entity = response.getEntity();

return entity != null ? EntityUtils.toString(entity) : null;
} else {
throw new ClientProtocolException(
"Unexpected response status: " + status);
}
}
};
String responseBody = httpclient.execute(httpPost, responseHandler);
System.out.println("----------------------------------------");
System.out.println(responseBody);

} catch (Exception e) {
System.out.println(e);
}

}

public static void main(String[] args) {
JSONObject obj = new JSONObject();
obj.put("UpdateCode", "update");
obj.put("harmful_info_id", "014e79cb198579840a504c89cb17c10f");
obj.put("harmful_info_desc", "新疆&暴");
post(obj, "http://10.10.40.3:5002/updateKeywordRules");
}
}

Java 通过HttpClient Post方式提交json请求的更多相关文章

  1. [java,2018-01-16] HttpClient发送、接收 json 请求

    最近需要用到许多在后台发送http请求的功能,可能需要发送json和xml类型的数据. 就抽取出来写了一个帮助类: 首先判断发送的数据类型是json还是xml: import org.dom4j.Do ...

  2. java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例

    java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例HttpClient 测试类,提供get post方法实例 package com.zdz.httpclient; i ...

  3. 前端ajax用post方式提交json数据给后端时,网络报错 415

    项目框架:spring+springmvc+mybatis 问题描述:前端ajax用post方式提交json数据给后端时,网络报错 415 前端异常信息:Failed to load resource ...

  4. 利用SparkLauncher 类以JAVA API 编程的方式提交Spark job

    一.环境说明和使用软件的版本说明: hadoop-version:hadoop-2.9.0.tar.gz spark-version:spark-2.2.0-bin-hadoop2.7.tgz jav ...

  5. ②HttpURLConnection通过Json参数方式提交Post请求

    之前的文章介绍过通过报文的方式HttpURLConnection提交post请求,今天介绍下通过Json参数的方法提交Post请求,先上代码 public static HttpResponse se ...

  6. Android 使用HttpClient方式提交POST请求

    final String username = usernameEditText.getText().toString().trim(); final String password = passwr ...

  7. Android 使用HttpClient方式提交GET请求

    public void httpClientGet(View view) { final String username = usernameEditText.getText().toString() ...

  8. 导出excel时,以form方式提交json数据

    今天在写项目时写到一个excel的导出,开始想用ajax请求后台后导出,但发现ajax会有返回值,而且ajax无法直接输出文件,而后台的excel导出方法已经封装好,不方便修改. 就改用了提交的方式f ...

  9. Java实现HttpClient发送GET、POST请求(https、http)

    1.引入相关依赖包 jar包下载:httpcore4.5.5.jar    fastjson-1.2.47.jar maven: <dependency> <groupId>o ...

随机推荐

  1. log sum of exponential

    The so-called “log sum of exponentials” is a functional form commonly encountered in dynamic discret ...

  2. python学习-61 类

    类 (在python2里) 1.属性 ---数据属性 ---函数属性 查看属性字典 class chinese: rz:'huangzhong' print(chinese.__dict__) 运行结 ...

  3. lmir 随笔

    近期需要研究一些特征工程的工作,就打算把微软之前公布出来的特征都复现一遍,今天遇到的特征是 LMIR, 其实也就是language model for information retrieval的简写 ...

  4. [LOJ2065] [SDOI2016]模式字符串

    题目链接 洛谷:https://www.luogu.org/problemnew/show/P4075 LOJ:https://loj.ac/problem/2065 Solution 这种题看起来就 ...

  5. 【.Net Core】编译时禁止自动生成netcoreapp文件夹

    原文:[.Net Core]编译时禁止自动生成netcoreapp文件夹 每次在编译生成文件时,VS都会自动在<OutputPath>属性指定的路劲后再追加一个用NetCore命名的文件夹 ...

  6. java之初识hibernate

    1. 使用jdbc进行数据库操作:获取数据库连接,编写sql语句,执行sql操作,关闭连接. 比如:每次创建连接,释放资源----使的执行效率降低: 解决方案:连接池. 编写sql语句动作----简单 ...

  7. StatusStrip控件的使用(转:http://blog.sina.com.cn/s/blog_4f18c3ec0100fguf.html)

    c# winForm 将窗体状态栏StatusStrip 分成左中右三部分 右边显示当前时间 实现效果: 通过StatusStrip显示窗体状态栏 同时将状态栏分成三部分 居左边显示相关文字信息 中间 ...

  8. Ubuntu 18.04 LTS版本 GIMP安装

    为何安装? GIMP全称GNU Image Manipulation Program,是Linux下的一款图片处理软件,功能基本完备,用户体验不错. 如何安装? 命令:sudo apt install ...

  9. 【转载】Asp.net网站安全:去除网站根目录下的备份文件防止代码泄露

    很多网站运维人员在更新网站版本的时候,喜欢直接在网站目录文件夹中直接压缩原来的网站文件,如果这个备份压缩文件没有移动出去,这样是非常不安全的,有些网站攻击者可能会尝试访问你网站下有没有对应名字的压缩备 ...

  10. 208道Java常见的面试题

    一.Java 基础 1.JDK 和 JRE 有什么区别? JRE=JVM+各种基础类库+java类库(String\System) JDK>JRE>JVM JRE:是java运行时环境  ...