http发送post请求,发送参数为实体

public static String post(JSONObject json, String url) {

String result = "";

HttpPost post = new HttpPost(url);

try {

CloseableHttpClient httpClient = HttpClients.createDefault(); post.setHeader("Content-Type", "application/json;charset=utf-8"); post.addHeader("Authorization", "Basic YWRtaW46");

StringEntity postingString = new StringEntity(json.toString(), "utf-8"); post.setEntity(postingString);

HttpResponse response = httpClient.execute(post);

InputStream in = response.getEntity().getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));

StringBuilder strber = new StringBuilder();

String line = null;

while ((line = br.readLine()) != null) {

strber.append(line + '\n'); }

br.close();

in.close(); result = strber.toString(); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {

result = "服务器异常"; } } catch (Exception e) { System.out.println("请求异常");

throw new RuntimeException(e);

} finally { post.abort(); }

return result; }

EntityDto dto= new EntityDto ();

dto.setUname("哈哈");

dto.setAge(18);

dto.setSalary(12000);

String response = HttpUtil2.post((JSONObject) JSONObject.toJSON(autoTestDto), autoTestUrl);

相关推荐
modelmd3 分钟前
研究C语言的hello world输出
c语言·开发语言·chrome
小小19926 分钟前
vue 单页面请求
开发语言·前端·javascript
hhb_6186 分钟前
JavaScript 本地存储与动态数据渲染实战案例
开发语言·javascript·ecmascript
许彰午7 分钟前
CacheSQL:一个面向政务系统的内存缓存数据库中间件
java·数据库·缓存·中间件·面试·开源软件·政务
YaBingSec9 分钟前
玄机网络安全靶场:Apache HTTPD 解析漏洞(CVE-2017-15715)WP
java·笔记·安全·web安全·php·apache
淀粉肠kk13 分钟前
【C++11】智能指针详解
开发语言·c++
书源丶17 分钟前
三十二、Java集合(一)——Collection与List全家桶
java·windows·list
敖正炀18 分钟前
Spring 深度内核-核心容器与扩展机制-SpringFactoriesLoader 到 AutoConfiguration.imports:插件化演进
spring
敖正炀20 分钟前
Spring 深度内核-核心容器与扩展机制-类型转换与数据绑定体系:ConversionService、PropertyEditor
spring
kyriewen1123 分钟前
Next.js部署:从本地跑得欢,到线上飞得稳
开发语言·前端·javascript·科技·react.js·前端框架·ecmascript