|
|
@ -1,148 +0,0 @@
|
|
|
|
package weaver.interfaces.util;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @version 1.0
|
|
|
|
|
|
|
|
* @Title ecology-9
|
|
|
|
|
|
|
|
* @Company 泛微软件
|
|
|
|
|
|
|
|
* @CreateDate 2022/11/16
|
|
|
|
|
|
|
|
* @Description ${description}
|
|
|
|
|
|
|
|
* @Author Lee
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
|
|
|
import com.customization.sendtodo.HttpRequestUtil;
|
|
|
|
|
|
|
|
import org.apache.commons.collections.MapUtils;
|
|
|
|
|
|
|
|
import org.apache.http.HttpResponse;
|
|
|
|
|
|
|
|
import org.apache.http.HttpStatus;
|
|
|
|
|
|
|
|
import org.apache.http.client.HttpClient;
|
|
|
|
|
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
|
|
|
|
|
import org.apache.http.conn.ConnectTimeoutException;
|
|
|
|
|
|
|
|
import org.apache.http.entity.StringEntity;
|
|
|
|
|
|
|
|
import org.apache.http.impl.client.DefaultHttpClient;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
|
|
|
import java.net.*;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class HttpUtils2 {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws IOException {
|
|
|
|
|
|
|
|
HashMap<String, Object> requestMap = new HashMap<>();
|
|
|
|
|
|
|
|
requestMap.put("appKey", "2022111517310001-0m7929Wan81B7");
|
|
|
|
|
|
|
|
requestMap.put("appSecret", "f2blp7rpMb1iZbeb7Xv3eu6TtxbACW");
|
|
|
|
|
|
|
|
// requestMap.put("appKey", "D20220926160400001-4t08Rhu3l94");
|
|
|
|
|
|
|
|
// requestMap.put("appSecret", "080Jc0z4Bi0afVv93802e8UmHR2lVq");
|
|
|
|
|
|
|
|
// JSONObject jsonObject = new JSONObject(requestMap);
|
|
|
|
|
|
|
|
// String jsonString = JSONObject.toJSONString(requestMap);
|
|
|
|
|
|
|
|
// System.out.println(jsonString);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String s = HttpUtils.ssoLoginDoPost("https://sapp-test-idito.dito.ph:30443/open-apis/auth/v1/oauth/app_access_token", requestMap);
|
|
|
|
|
|
|
|
// String s = HttpUtils.ssoLoginDoPost("http://10.10.178.100/open-apis/auth/v1/oauth/app_access_token", requestMap);
|
|
|
|
|
|
|
|
System.out.println(s);
|
|
|
|
|
|
|
|
// String s = HttpRequestUtil.doPostByAuth("https://10.10.178.100/open-apis/auth/v1/oauth/app_access_token", jsonObject.toString(), null);
|
|
|
|
|
|
|
|
// System.out.println(s);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 向指定 URL 发送GET方法的请求
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param url 发送请求的 URL
|
|
|
|
|
|
|
|
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
|
|
|
|
|
|
|
|
* @return 所代表远程资源的响应结果
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static String doGet(String url, String param) {
|
|
|
|
|
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
|
|
|
|
|
BufferedReader in = null;
|
|
|
|
|
|
|
|
String urlNameString = url;
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
if (param != null || param != "") {
|
|
|
|
|
|
|
|
urlNameString = urlNameString + "?" + param;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
URL realUrl = new URL(urlNameString);
|
|
|
|
|
|
|
|
URLConnection connection = realUrl.openConnection();
|
|
|
|
|
|
|
|
connection.setRequestProperty("accept", "*/*");
|
|
|
|
|
|
|
|
connection.setRequestProperty("connection", "Keep-Alive");
|
|
|
|
|
|
|
|
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
|
|
|
|
|
|
connection.connect();
|
|
|
|
|
|
|
|
// 定义 BufferedReader输入流来读取URL的响应
|
|
|
|
|
|
|
|
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
|
|
|
|
|
|
String line;
|
|
|
|
|
|
|
|
while ((line = in.readLine()) != null) {
|
|
|
|
|
|
|
|
result.append(line);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (ConnectException e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
} catch (SocketTimeoutException | ConnectTimeoutException e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
//SocketTimeoutException:是Java包下抛出的异常,这定义了Socket读数据的超时时间,即从server获取响应数据须要等待的时间;当读取或者接收Socket超时会抛出SocketTimeoutException
|
|
|
|
|
|
|
|
System.out.println("sendGet SocketTimeoutException, url=" + url + ",param=" + param);
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
System.out.println("sendGet IOException, url=" + url + ",param=" + param);
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
System.out.println("sendGet Exception, url=" + url + ",param=" + param);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
if (in != null) {
|
|
|
|
|
|
|
|
in.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
|
|
|
ex.printStackTrace();
|
|
|
|
|
|
|
|
System.out.println("sendGet Exception, url=" + url + ",param=" + param);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.toString();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* post请求
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param path url地址
|
|
|
|
|
|
|
|
* @param Info jsonobject 参数
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
* @throws IOException
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static String doPost(String path, JSONObject Info, Map<String, String> headers) throws IOException {
|
|
|
|
|
|
|
|
HttpClient client = new DefaultHttpClient();
|
|
|
|
|
|
|
|
HttpPost post = new HttpPost(path);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
post.setHeader("Content-Type", "application/json");
|
|
|
|
|
|
|
|
if (MapUtils.isNotEmpty(headers)) {
|
|
|
|
|
|
|
|
for (String key : headers.keySet()) {
|
|
|
|
|
|
|
|
post.addHeader(key, headers.get(key));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
String result = "";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
StringEntity s = new StringEntity(Info.toString(), "utf-8");
|
|
|
|
|
|
|
|
s.setContentEncoding("application/json");
|
|
|
|
|
|
|
|
post.setEntity(s);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 发送请求
|
|
|
|
|
|
|
|
HttpResponse httpResponse = client.execute(post);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取响应输入流
|
|
|
|
|
|
|
|
InputStream inStream = httpResponse.getEntity().getContent();
|
|
|
|
|
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "utf-8"));
|
|
|
|
|
|
|
|
StringBuilder strber = new StringBuilder();
|
|
|
|
|
|
|
|
String line = null;
|
|
|
|
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
|
|
|
|
strber.append(line + "\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
inStream.close();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
result = strber.toString();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
System.out.println("请求异常");
|
|
|
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|