相关文章推荐
不羁的南瓜  ·  httpClient ...·  1 周前    · 
侠义非凡的沙滩裤  ·  Java ...·  1 年前    · 
千杯不醉的红烧肉  ·  ATT 汇编 lea 和 ...·  1 年前    · 
import java .io .IOException ; import java .io .InputStream ; import java .io .OutputStream ; import java .net .HttpURLConnection ; import java .net .URL ; import java .util .ArrayList ; import java .util .List ; import org .apache .http .HttpEntity ; import org .apache .http .HttpResponse ; import org .apache .http .NameValuePair ; import org .apache .http .client .ClientProtocolException ; import org .apache .http .client .ResponseHandler ; import org .apache .http .client .entity .UrlEncodedFormEntity ; import org .apache .http .client .methods .CloseableHttpResponse ; 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 .message .BasicNameValuePair ; import org .apache .http .params .CoreProtocolPNames ; import org .apache .http .util .EntityUtils ; import org .json .JSONArray ; import org .json .JSONException ; import org .json .JSONObject ; public class PostXml { public static void main(String args[]) { try { JSONObject obj = new JSONObject() ; obj .append ( "app_name" , "全民大讨论" ) ; obj .append ( "app_ip" , "10.21.243.234" ) ; obj .append ( "app_port" , 8080 ) ; obj .append ( "app_type" , "001" ) ; obj .append ( "app_area" , "asd" ) ; CloseableHttpClient httpclient = HttpClients .createDefault () ; System .out .println (obj) ; HttpPost httpPost = new HttpPost( "http://119.29.85.118//test.php" ) ; httpPost .addHeader ( "Content-Type" , "application/json;charset=UTF-8" ) ; // 解决中文乱码问题 StringEntity stringEntity = new StringEntity(obj .toString (), "UTF-8" ) ; stringEntity .setContentEncoding ( "UTF-8" ) ; httpPost .setEntity (stringEntity) ; // CloseableHttpResponse response = // httpclient .execute (httpPost) ; System .out .println ( "Executing request " + httpPost .getRequestLine ()) ; // Create a custom response handler 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) ;

服务端test.php代码:

$result = file_get_contents( 'php://input' ); echo $result ; echo json_decode(json_encode( $result ));

输出结果:
{“app_name”:[“全民大讨论”],”app_ip”:[“10.21.243.234”],”app_type”:[“001”],”app_port”:[8080],”app_area”:[“asd”]}

Executing request POST http://119.29.85.118//test.php HTTP/1.1

{“app_name”:[“全民大讨论”],”app_ip”:[“10.21.243.234”],”app_type”:[“001”],”app_port”:[8080],”app_area”:[“asd”]}{“app_name”:[“全民大讨论”],”app_ip”:[“10.21.243.234”],”app_type”:[“001”],”app_port”:[8080],”app_area”:[“asd”]}

import com.alibaba.fast json . JSON ; import com.alibaba.fast json . JSON Object; import org.apache.http.HttpResponse; import org.apache.http.client.methods.Http Post ; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClie. 我这里使用的版本是 httpclient -4.5版本的。通过 Post 请求 返回 Byte数组。对数组进行读就行了 import java .io.IOException; import java .util.ArrayList; import java .util.List; import java .util.Map; import org.apache.http.HttpEntity; import or public static JSON Object do Post (String url, JSON Object json ){ Closeable HttpClient httpclient = HttpClient Builder.create().build(); 使用 post man工具发送multipart/form-data带有 Json 文件的 Post 请求,文件内容其实就是 json 字符串,这种请求通过 post man发送,他给你处理,但是你需要做接口化测试就得偶用代码来实现,不是使用他的工具,就需要你自己写代码了 1、首先弄清楚你需要得格式: 像我这个这种格式就是可以的(关于文件类型) 这个最难最重要的就是处理上面的这个文件。 需要的两个jar包,我是... java 相关: httpclient 模拟 post 请求 json 封装表单 数据 的实现方法发布于 2020-7-29|复制链接下面小妖就为大家带来一篇 httpclient 模拟 post 请求 json 封装表单 数据 的实现方法。小妖觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小妖过来看看吧废话不说上代码:``` java public static String http Post With JSON (Strin...