HttpUtil.java
2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.air.util;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import org.omg.CORBA.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.net.www.protocol.http.HttpURLConnection;
import sun.rmi.runtime.Log;
import javax.xml.ws.Response;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;
/**
* Created by XYH on 2019/1/22.
*/
public class HttpUtil {
static Logger logger = LoggerFactory.getLogger(HttpUtil.class);
public static String sendData(String url, String SAMLResponse,String providerId) {
StringBuilder str = new StringBuilder();
BufferedReader bf = null;
OutputStreamWriter writer = null;
String param= null;
String param2=null;
try {
param = "SAMLResponse="+ URLEncoder.encode(SAMLResponse,"UTF-8")+"&"+"providerId="+providerId;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
URL Url = new URL(url);
HttpURLConnection conn = (HttpURLConnection) Url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
//conn.setRequestProperty("X-Rrquested-With", "XMLHttpRequest");
//conn.setRequestProperty("Connection", "keep-alive");
//conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
conn.setRequestMethod("POST");
conn.connect();
StringBuffer params = new StringBuffer();
//表单参数与get形式一样
//params.append("SAMLResponse").append("=").append(SAMLResponse).append("&")
// .append("providerId").append("=").append(providerId);
writer = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
writer.write(param);
writer.flush();
bf = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String inputLine = null;
while ((inputLine = bf.readLine()) != null) {
str.append(inputLine);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
writer.close();
bf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return str.toString();
}
}