|
@@ -1,8 +1,8 @@
|
|
|
package com.minpay.common.util;
|
|
|
|
|
|
-import java.io.BufferedReader;
|
|
|
-import java.io.InputStreamReader;
|
|
|
-import java.io.PrintWriter;
|
|
|
+import com.startup.minpay.util.Log;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
import java.net.HttpURLConnection;
|
|
|
import java.net.URL;
|
|
|
import java.net.URLConnection;
|
|
@@ -71,4 +71,54 @@ public class HttpPost {
|
|
|
System.out.println("result:"+result);
|
|
|
return result;
|
|
|
}
|
|
|
+ /**
|
|
|
+ * 发送post请求(推货专用)
|
|
|
+ * @param url
|
|
|
+ * @param param "a=111&b=123"
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String sendPostFormachine(String url, String param){
|
|
|
+ String result = "";
|
|
|
+ try{
|
|
|
+ param = URLEncoder.encode(param, "utf-8");
|
|
|
+ URL httpurl = new URL(url);
|
|
|
+ HttpURLConnection httpConn = (HttpURLConnection)httpurl.openConnection();
|
|
|
+ httpConn.setDoOutput(true);
|
|
|
+ httpConn.setDoInput(true);
|
|
|
+ // 设置传递方式
|
|
|
+ httpConn.setRequestMethod("POST");
|
|
|
+ // 设置维持长连接
|
|
|
+ httpConn.setRequestProperty("Connection", "Keep-Alive");
|
|
|
+ // 设置文件字符集:
|
|
|
+ httpConn.setRequestProperty("Charset", "UTF-8");
|
|
|
+ // 转换为字节数组
|
|
|
+ //byte[] data = ("formData=" + param.toString()).getBytes();
|
|
|
+ byte[] data = (param.toString()).getBytes();
|
|
|
+ // 设置文件长度
|
|
|
+ httpConn.setRequestProperty("Content-Length", String.valueOf(data.length));
|
|
|
+ // 设置文件类型:
|
|
|
+ httpConn.setRequestProperty("contentType", "application/json");
|
|
|
+// PrintWriter out = new PrintWriter(httpConn.getOutputStream());
|
|
|
+// out.print(param.getBytes());
|
|
|
+// out.flush();
|
|
|
+// out.close();
|
|
|
+ OutputStream out = new DataOutputStream(httpConn.getOutputStream()) ;
|
|
|
+ // 写入请求的字符串
|
|
|
+ out.write((param.toString()).getBytes());
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+
|
|
|
+ BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"UTF-8"));
|
|
|
+ String line;
|
|
|
+ while ((line = in.readLine())!= null){
|
|
|
+ result += line;
|
|
|
+ }
|
|
|
+ in.close();
|
|
|
+ }catch(Exception e){
|
|
|
+ System.out.println("error!!"+e);
|
|
|
+ }
|
|
|
+ Log.info("返回:".concat(result));
|
|
|
+ System.out.println("result:"+result);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
}
|