Article

HttpClient使用post方式模拟表单提交数据

java 复制代码
//组装url
String url = "http://192.168.0.105:8080/";
// 创建httpClient
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建post请求方式实例
HttpPost httpPost = new HttpPost(url);
List<NameValuePair>list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("tableName",tableName));
list.add(new BasicNameValuePair("fileName",fileName));
list.add(new BasicNameValuePair("path",path));
HttpEntity entity = new UrlEncodedFormEntity(list);//模拟form进行表单提交
httpPost.setEntity(entity);                               //banding内容
HttpResponse response = httpClient.execute(httpPost); //连接服务器
HttpEntity entit =  response.getEntity(); //获取内容
return EntityUtils.toString(entit, "utf-8");
评论
0 条评论