1
|
1 package de.mpiwg.itgroup.ismi.utils;
|
|
2
|
|
3 import java.io.BufferedReader;
|
|
4 import java.io.IOException;
|
|
5 import java.io.InputStream;
|
|
6 import java.io.InputStreamReader;
|
|
7 import java.net.HttpURLConnection;
|
|
8 import java.net.URL;
|
|
9 import java.net.URLConnection;
|
|
10 import java.security.KeyManagementException;
|
|
11 import java.security.NoSuchAlgorithmException;
|
|
12 import java.security.cert.X509Certificate;
|
|
13
|
|
14 import javax.net.ssl.HttpsURLConnection;
|
|
15 import javax.net.ssl.SSLContext;
|
|
16 import javax.net.ssl.SSLSocketFactory;
|
|
17 import javax.net.ssl.TrustManager;
|
|
18 import javax.xml.parsers.DocumentBuilderFactory;
|
|
19
|
|
20 import org.apache.commons.io.IOUtils;
|
|
21 import org.w3c.dom.Document;
|
|
22
|
|
23 import javax.net.ssl.TrustManager;
|
|
24 import javax.net.ssl.X509TrustManager;
|
|
25
|
|
26
|
|
27 public class HTTPUtils {
|
|
28
|
|
29
|
|
30 public static Document getDocument(String link){
|
|
31 try {
|
|
32 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(link);
|
|
33 return doc;
|
|
34 } catch (Exception e) {
|
|
35 e.printStackTrace();
|
|
36 }
|
|
37 return null;
|
|
38 }
|
|
39
|
|
40 public static String getResponse(String link) throws IOException{
|
|
41
|
|
42 URL url = new URL(link);
|
|
43 URLConnection uc = url.openConnection();
|
|
44 BufferedReader in = new BufferedReader(
|
|
45 new InputStreamReader(
|
|
46 uc.getInputStream()));
|
|
47 String inputLine;
|
|
48 StringBuilder sb = new StringBuilder();
|
|
49 while ((inputLine = in.readLine()) != null)
|
|
50 sb.append(inputLine);
|
|
51 in.close();
|
|
52
|
|
53 return null;
|
|
54 }
|
|
55
|
|
56
|
|
57 final static TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
|
|
58 @Override
|
|
59 public void checkClientTrusted( final X509Certificate[] chain, final String authType ) {
|
|
60 }
|
|
61 @Override
|
|
62 public void checkServerTrusted( final X509Certificate[] chain, final String authType ) {
|
|
63 }
|
|
64 @Override
|
|
65 public X509Certificate[] getAcceptedIssuers() {
|
|
66 return null;
|
|
67 }
|
|
68 } };
|
|
69
|
|
70 public static HttpResponse getHttpSSLResponse(String link) throws IOException, KeyManagementException, NoSuchAlgorithmException{
|
|
71
|
|
72 // Install the all-trusting trust manager
|
|
73 final SSLContext sslContext = SSLContext.getInstance( "SSL" );
|
|
74 sslContext.init( null, trustAllCerts, new java.security.SecureRandom() );
|
|
75 // Create an ssl socket factory with our all-trusting manager
|
|
76 final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
|
|
77
|
|
78 /////
|
|
79
|
|
80 URL url = new URL(link);
|
|
81 HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
|
|
82
|
|
83 ( (HttpsURLConnection) httpConn ).setSSLSocketFactory( sslSocketFactory );
|
|
84
|
|
85
|
|
86 InputStream in;
|
|
87 if (httpConn.getResponseCode() >= 400) {
|
|
88 in = httpConn.getErrorStream();
|
|
89 } else {
|
|
90 in = httpConn.getInputStream();
|
|
91 }
|
|
92
|
|
93 return new HttpResponse(httpConn.getResponseCode(), in);
|
|
94 }
|
|
95
|
|
96
|
|
97 public static HttpStringResponse getHttpSSLStringResponse(String link) throws IOException, KeyManagementException, NoSuchAlgorithmException{
|
|
98
|
|
99 // Install the all-trusting trust manager
|
|
100 final SSLContext sslContext = SSLContext.getInstance( "SSL" );
|
|
101 sslContext.init( null, trustAllCerts, new java.security.SecureRandom() );
|
|
102 // Create an ssl socket factory with our all-trusting manager
|
|
103 final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
|
|
104
|
|
105 URL url = new URL(link);
|
|
106 HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
|
|
107
|
|
108 ( (HttpsURLConnection) httpConn ).setSSLSocketFactory( sslSocketFactory );
|
|
109
|
|
110
|
|
111 BufferedReader in = null;
|
|
112 if (httpConn.getResponseCode() >= 400) {
|
|
113 in = new BufferedReader(
|
|
114 new InputStreamReader(
|
|
115 httpConn.getErrorStream()));
|
|
116 } else {
|
|
117 in = new BufferedReader(
|
|
118 new InputStreamReader(
|
|
119 httpConn.getInputStream()));
|
|
120 }
|
|
121
|
|
122
|
|
123 String inputLine;
|
|
124 StringBuilder sb = new StringBuilder();
|
|
125 while ((inputLine = in.readLine()) != null)
|
|
126 sb.append(inputLine + "\n");
|
|
127 in.close();
|
|
128
|
|
129 return new HttpStringResponse(httpConn.getResponseCode(), sb.toString());
|
|
130 }
|
|
131
|
|
132 public static HttpStringResponse getHttpStringResponse(String link) throws IOException{
|
|
133
|
|
134 //System.out.println(link);
|
|
135
|
|
136 URL url = new URL(link);
|
|
137 HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
|
|
138
|
|
139 BufferedReader in = null;
|
|
140 if (httpConn.getResponseCode() >= 400) {
|
|
141 in = new BufferedReader(
|
|
142 new InputStreamReader(
|
|
143 httpConn.getErrorStream()));
|
|
144 } else {
|
|
145 in = new BufferedReader(
|
|
146 new InputStreamReader(
|
|
147 httpConn.getInputStream()));
|
|
148 }
|
|
149
|
|
150
|
|
151 String inputLine;
|
|
152 StringBuilder sb = new StringBuilder();
|
|
153 while ((inputLine = in.readLine()) != null)
|
|
154 sb.append(inputLine + "\n");
|
|
155 in.close();
|
|
156
|
|
157 return new HttpStringResponse(httpConn.getResponseCode(), sb.toString());
|
|
158 }
|
|
159
|
|
160 public static class HttpStringResponse{
|
|
161 public int code;
|
|
162 public String content;
|
|
163 public HttpStringResponse(int code, String content){
|
|
164 this.code = code;
|
|
165 this.content = content;
|
|
166 }
|
|
167 }
|
|
168
|
|
169 public static class HttpResponse{
|
|
170 public int code;
|
|
171 public InputStream content;
|
|
172 public HttpResponse(int code, InputStream content){
|
|
173 this.code = code;
|
|
174 this.content = content;
|
|
175 }
|
|
176 }
|
|
177 }
|