post接口
代码
下面展示一些 内联代码片
。
// A code block
net.sf.json.JSONObject resultsBack6 = new net.sf.json.JSONObject();
String url6 = "";
Map<String,String> params2=new HashMap<>();
params2.put("TransactionCode","GMOP.AP.TPM.GetMaintainPlanDataforEAP");
params2.put("ProjectCode","tpm");
params2.put("Params", "{\"Bases\":{\"InstanceCode\":\"QD_SIP_MES\"}}");
//java对象变成json对象
net.sf.json.JSONObject jsonObject2= net.sf.json.JSONObject.fromObject(params2);
//json对象转换成json字符串
String jsonStr2=jsonObject2.toString();
String resultString6 = this.doHttpPost(url6, jsonStr2);
if (null == resultString6 || "error".equals(resultString6) || "".equals(resultString6)) {
System.out.println("调用不成功");
} else {
resultString6 = resultString6.replaceAll("<", "<").replaceAll(">", ">").replaceAll("\n", "").replaceAll("\\u000d","").replaceAll("\\u000a","");
net.sf.json.JSONObject jsonObject8 = resultsBack6.fromObject(resultString6);
String resultValue2 = jsonObject8.getString("ResultValue").trim();
net.sf.json.JSONObject resultsBack9 = new net.sf.json.JSONObject();
net.sf.json.JSONObject jsonObject9 = resultsBack9.fromObject(resultValue2);
String bases = jsonObject9.getString("Bases").trim();
net.sf.json.JSONObject resultsBack10 = new net.sf.json.JSONObject();
net.sf.json.JSONObject jsonObject10 = resultsBack10.fromObject(bases);
net.sf.json.JSONArray planMaintainEQPs = jsonObject10.getJSONArray("PlanMaintainEQPs");
net.sf.json.JSONArray realMaintainEQPs = jsonObject10.getJSONArray("RealMaintainEQPs");
```}
```javascript
// An highlighted block
net.sf.json.JSONObject resultsBack6 = new net.sf.json.JSONObject();
String url6 = "http://10.12.0.68:8019/RestRequestService/RestCloud";
Map<String,String> params2=new HashMap<>();
params2.put("TransactionCode","GMOP.AP.TPM.GetMaintainPlanDataforEAP");
params2.put("ProjectCode","tpm");
params2.put("Params", "{\"Bases\":{\"InstanceCode\":\"QD_SIP_MES\"}}");
//java对象变成json对象
net.sf.json.JSONObject jsonObject2= net.sf.json.JSONObject.fromObject(params2);
//json对象转换成json字符串
String jsonStr2=jsonObject2.toString();
String resultString6 = this.doHttpPost(url6, jsonStr2);
if (null == resultString6 || "error".equals(resultString6) || "".equals(resultString6)) {
System.out.println("设备保养接口调用不成功");
} else {
resultString6 = resultString6.replaceAll("<", "<").replaceAll(">", ">").replaceAll("\n", "").replaceAll("\\u000d","").replaceAll("\\u000a","");
net.sf.json.JSONObject jsonObject8 = resultsBack6.fromObject(resultString6);
String resultValue2 = jsonObject8.getString("ResultValue").trim();
net.sf.json.JSONObject resultsBack9 = new net.sf.json.JSONObject();
net.sf.json.JSONObject jsonObject9 = resultsBack9.fromObject(resultValue2);
String bases = jsonObject9.getString("Bases").trim();
net.sf.json.JSONObject resultsBack10 = new net.sf.json.JSONObject();
net.sf.json.JSONObject jsonObject10 = resultsBack10.fromObject(bases);
net.sf.json.JSONArray planMaintainEQPs = jsonObject10.getJSONArray("PlanMaintainEQPs");
net.sf.json.JSONArray realMaintainEQPs = jsonObject10.getJSONArray("RealMaintainEQPs");
}
public String doHttpPost(String path,String data) {
String str = "";
try {
URL url = new URL(path);
//打开和url之间的连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
PrintWriter out = null;
/**设置URLConnection的参数和普通的请求属性****start***/
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
/**设置URLConnection的参数和普通的请求属性****end***/
//设置是否向httpUrlConnection输出,设置是否从httpUrlConnection读入,此外发送post请求必须设置这两个
//最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet,
//post与get的 不同之处在于post的参数不是放在URL字串里面,而是放在http请求的正文内。
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");//GET和POST必须全大写
/**GET方法请求*****start*/
/**
* 如果只是发送GET方式请求,使用connet方法建立和远程资源之间的实际连接即可;
* 如果发送POST方式的请求,需要获取URLConnection实例对应的输出流来发送请求参数。
* */
// conn.connect();
/**GET方法请求*****end*/
/***POST方法请求****start*/
out = new PrintWriter(conn.getOutputStream());//获取URLConnection对象对应的输出流
out.print(data);//发送请求参数即数据
out.flush();//缓冲数据
/***POST方法请求****end*/
//获取URLConnection对象对应的输入流
InputStream is = conn.getInputStream();
//构造一个字符流缓存
BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8"));
// while ((str = br.readLine()) != null) {
// str=new String(str.getBytes(),"UTF-8");//解决中文乱码问题
//
//
// }
String line;
while ((line = br.readLine()) != null) {
str += line;
}
//关闭流
is.close();
//断开连接,最好写上,disconnect是在底层tcp socket链接空闲时才切断。如果正在被其他线程使用就不切断。
//固定多线程的话,如果不disconnect,链接会增多,直到收发不出信息。写上disconnect后正常一些。
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}