generated from root/miduo_server
post请求
parent
a50144cc60
commit
e4e3f5cb71
|
@ -20,7 +20,7 @@ public class KTGetUserOpenIdController extends HttpServlet {
|
|||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(KTGetUserOpenIdController.class);
|
||||
|
||||
public static String login_url = "http://api.kt007.com/api/v1/verify/user?";
|
||||
public static String login_url = "http://api.kt007.com/api/v1/verify/user";
|
||||
public static String appkey = "vNCn3fkgZHt6wxGYl9IQUJo0R4LKPjW2";
|
||||
|
||||
public KTGetUserOpenIdController() {
|
||||
|
@ -68,15 +68,16 @@ public class KTGetUserOpenIdController extends HttpServlet {
|
|||
sbKey.append(appkey);
|
||||
String mySign = MD5Util.encrypByMd5(sbKey.toString());
|
||||
params.put("sign", mySign);
|
||||
StringBuilder httpUrl = new StringBuilder();
|
||||
httpUrl.append(login_url).append("gid").append("=").append(gid).append("&")
|
||||
.append("pid").append("=").append(pid).append("&")
|
||||
.append("tm").append("=").append(tm).append("&")
|
||||
.append("access_token").append("=").append(token).append("&")
|
||||
.append("sign").append("=").append(mySign);
|
||||
LOGGER.info("KTGetUserOpenIdController request={}", httpUrl.toString());
|
||||
String loginResult = HttpUtils.httpRequest(httpUrl.toString());
|
||||
JSONObject param = new JSONObject();
|
||||
for (Map.Entry<String, String> entry : params.entrySet()) {
|
||||
param.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
String loginResult = HttpUtils.sendPost(login_url, param);
|
||||
LOGGER.info("KTGetUserOpenIdController 加密前={} 加密后={} 请求结果={}", sbKey.toString(), mySign, loginResult);
|
||||
|
||||
loginResult = HttpUtils.doPost(login_url, params);
|
||||
LOGGER.info("二次请求 : KTGetUserOpenIdController 加密前={} 加密后={} 请求结果={}", sbKey.toString(), mySign, loginResult);
|
||||
|
||||
if (loginResult == null || loginResult.isEmpty()) {
|
||||
returnErrorToFront(res, response, "token is empty");
|
||||
return;
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.ljsd.util;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
|
@ -10,13 +11,11 @@ import org.apache.http.impl.client.HttpClients;
|
|||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
@ -50,7 +49,7 @@ public class HttpUtils {
|
|||
}
|
||||
|
||||
|
||||
public static String httpRequest(String httpUrl){
|
||||
public static String httpGetRequest(String httpUrl){
|
||||
HttpURLConnection connection = null;
|
||||
InputStream is = null;
|
||||
BufferedReader br = null;
|
||||
|
@ -107,4 +106,71 @@ public class HttpUtils {
|
|||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 向指定 URL 发送POST方法的请求
|
||||
*
|
||||
* @param url
|
||||
* 发送请求的 URL
|
||||
* @param param
|
||||
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
|
||||
* @return 所代表远程资源的响应结果
|
||||
*/
|
||||
public static String sendPost(String url, JSONObject param) {
|
||||
PrintWriter out = null;
|
||||
BufferedReader in = null;
|
||||
String result = "";
|
||||
try {
|
||||
URL realUrl = new URL(url);
|
||||
// 打开和URL之间的连接
|
||||
URLConnection conn = realUrl.openConnection();
|
||||
// 设置通用的请求属性
|
||||
conn.setRequestProperty("accept", "*/*");
|
||||
conn.setRequestProperty("connection", "Keep-Alive");
|
||||
conn.setRequestProperty("user-agent",
|
||||
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
||||
// 发送POST请求必须设置如下两行
|
||||
conn.setDoOutput(true);
|
||||
conn.setDoInput(true);
|
||||
// 获取URLConnection对象对应的输出流
|
||||
out = new PrintWriter(conn.getOutputStream());
|
||||
// 发送请求参数
|
||||
out.print(param);
|
||||
// flush输出流的缓冲
|
||||
out.flush();
|
||||
// 定义BufferedReader输入流来读取URL的响应
|
||||
in = new BufferedReader(
|
||||
new InputStreamReader(conn.getInputStream()));
|
||||
String line;
|
||||
while ((line = in.readLine()) != null) {
|
||||
result += line;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("发送 POST 请求出现异常!" + e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
// 使用finally块来关闭输出流、输入流
|
||||
finally {
|
||||
try {
|
||||
if (out != null) {
|
||||
out.close();
|
||||
}
|
||||
if (in != null) {
|
||||
in.close();
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue