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 package de.fub.mi.idenpa.httpclient;
27
28 import java.io.BufferedReader;
29 import java.io.ByteArrayInputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.InputStreamReader;
33 import java.net.URL;
34 import java.security.SecureRandom;
35 import java.security.cert.X509Certificate;
36
37 import javax.net.ssl.SSLContext;
38 import javax.net.ssl.SSLException;
39 import javax.net.ssl.SSLSession;
40 import javax.net.ssl.SSLSocket;
41 import javax.net.ssl.TrustManager;
42 import javax.net.ssl.X509TrustManager;
43
44 import org.apache.http.HttpResponse;
45 import org.apache.http.client.HttpClient;
46 import org.apache.http.client.methods.HttpGet;
47 import org.apache.http.client.methods.HttpPost;
48 import org.apache.http.client.methods.HttpUriRequest;
49 import org.apache.http.client.params.ClientPNames;
50 import org.apache.http.conn.ClientConnectionManager;
51 import org.apache.http.conn.scheme.Scheme;
52 import org.apache.http.conn.scheme.SchemeRegistry;
53 import org.apache.http.conn.ssl.AbstractVerifier;
54 import org.apache.http.conn.ssl.SSLSocketFactory;
55 import org.apache.http.conn.ssl.X509HostnameVerifier;
56 import org.apache.http.entity.StringEntity;
57 import org.apache.http.impl.client.DefaultHttpClient;
58 import org.apache.http.impl.conn.SingleClientConnManager;
59 import org.apache.http.params.BasicHttpParams;
60 import org.apache.http.params.HttpParams;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
63
64
65
66
67 public class HTTPClient {
68
69
70 public static final Logger m_objLog = LoggerFactory.getLogger(HTTPClient.class);
71
72
73 private HttpClient m_objClient;
74
75
76 private HttpUriRequest m_objReq;
77
78
79 private HttpResponse m_objResp;
80
81
82
83
84 public HTTPClient() {
85 m_objClient = getTolerantClient();
86 }
87
88
89
90
91
92
93 private DefaultHttpClient getTolerantClient() {
94 DefaultHttpClient result = null;
95
96 try {
97 SSLContext sslContext = SSLContext.getInstance("SSL");
98
99
100 sslContext.init(null, new TrustManager[] { new X509TrustManager() {
101 public X509Certificate[] getAcceptedIssuers() {
102 System.out.println("getAcceptedIssuers =============");
103 return null;
104 }
105
106 public void checkClientTrusted(X509Certificate[] certs,
107 String authType) {
108 System.out.println("checkClientTrusted =============");
109 }
110
111 public void checkServerTrusted(X509Certificate[] certs,
112 String authType) {
113 System.out.println("checkServerTrusted =============");
114 }
115 } }, new SecureRandom());
116
117 SSLSocketFactory sf = new SSLSocketFactory(sslContext);
118 sf.setHostnameVerifier(new X509HostnameVerifier() {
119
120 @Override
121 public boolean verify(String arg0, SSLSession arg1) {
122
123 return true;
124 }
125
126 @Override
127 public void verify(String arg0, String[] arg1, String[] arg2)
128 throws SSLException {
129
130
131 }
132
133 @Override
134 public void verify(String arg0, X509Certificate arg1) throws SSLException {
135
136
137 }
138
139 @Override
140 public void verify(String arg0, SSLSocket arg1) throws IOException {
141
142
143 }
144 });
145 Scheme httpsScheme = new Scheme("https", sf, 443);
146 SchemeRegistry schemeRegistry = new SchemeRegistry();
147 schemeRegistry.register(httpsScheme);
148
149 HttpParams params = new BasicHttpParams();
150 ClientConnectionManager cm = new SingleClientConnManager(params,
151 schemeRegistry);
152 result = new DefaultHttpClient(cm, params);
153 } catch (Throwable t) {
154 t.printStackTrace();
155 }
156 return result;
157 }
158
159
160
161
162
163
164
165 public String get(URL url) {
166 String result = null;
167
168 try {
169 m_objReq = new HttpGet(url.toString());
170 HttpParams params = m_objReq.getParams();
171 params.setParameter(ClientPNames.HANDLE_REDIRECTS, Boolean.FALSE);
172 m_objReq.setParams(params);
173 m_objResp = m_objClient.execute(m_objReq);
174 result = toString(m_objResp);
175 } catch (Exception e) {
176 e.printStackTrace();
177 }
178
179 return result;
180 }
181
182
183
184
185
186
187
188
189 public String postToStr(String url, String content) {
190 String result = null;
191
192 try {
193 m_objReq = new HttpPost(url);
194 ((HttpPost) m_objReq).setEntity(new StringEntity(content, "UTF-8"));
195 m_objResp = m_objClient.execute(m_objReq);
196 result = toString(m_objResp);
197 } catch (Exception e) {
198 e.printStackTrace();
199 }
200
201 return result;
202 }
203
204
205
206
207
208
209
210
211 public InputStream post(String url, String content) {
212 InputStream result = null;
213
214 try {
215 m_objReq = new HttpPost(url);
216 ((HttpPost) m_objReq).setEntity(new StringEntity(content, "UTF-8"));
217 m_objResp = m_objClient.execute(m_objReq);
218 String strResp = toString(m_objResp);
219 m_objLog.info("Got HTTP response with content: "+strResp);
220 result = new ByteArrayInputStream(strResp.getBytes());
221
222 } catch (Exception e) {
223 e.printStackTrace();
224 }
225
226 return result;
227 }
228
229
230
231
232
233
234
235
236 private String toString(HttpResponse resp) throws IOException {
237 String result = null;
238
239 if (resp != null && resp.getEntity() != null) {
240 InputStream in = resp.getEntity().getContent();
241 BufferedReader reader = new BufferedReader(
242 new InputStreamReader(in));
243 String line = null;
244 StringBuffer buffer = new StringBuffer();
245 while ((line = reader.readLine()) != null) {
246 buffer.append(line).append("\n");
247 }
248 result = buffer.toString();
249 }
250
251 return result;
252 }
253
254
255
256
257 class MyVerifier extends AbstractVerifier {
258
259
260 private final X509HostnameVerifier delegate;
261
262
263
264
265
266
267 public MyVerifier(final X509HostnameVerifier delegate) {
268 this.delegate = delegate;
269 }
270
271
272
273
274 @Override
275 public void verify(String host, String[] cns, String[] subjectAlts)
276 throws SSLException {
277
278
279
280
281
282
283
284 }
285 }
286 }