1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.ws; |
22 |
|
|
23 |
|
import java.io.BufferedReader; |
24 |
|
import java.io.File; |
25 |
|
import java.io.IOException; |
26 |
|
import java.io.InputStream; |
27 |
|
import java.io.InputStreamReader; |
28 |
|
import java.util.List; |
29 |
|
|
30 |
|
import org.apache.http.HttpEntity; |
31 |
|
import org.apache.http.HttpResponse; |
32 |
|
import org.apache.http.HttpVersion; |
33 |
|
import org.apache.http.NameValuePair; |
34 |
|
import org.apache.http.client.ClientProtocolException; |
35 |
|
import org.apache.http.client.HttpClient; |
36 |
|
import org.apache.http.client.entity.UrlEncodedFormEntity; |
37 |
|
import org.apache.http.client.methods.HttpPost; |
38 |
|
import org.apache.http.entity.mime.HttpMultipartMode; |
39 |
|
import org.apache.http.entity.mime.MultipartEntity; |
40 |
|
import org.apache.http.entity.mime.content.FileBody; |
41 |
|
import org.apache.http.entity.mime.content.InputStreamBody; |
42 |
|
import org.apache.http.entity.mime.content.StringBody; |
43 |
|
import org.apache.http.impl.client.DefaultHttpClient; |
44 |
|
import org.apache.http.params.BasicHttpParams; |
45 |
|
import org.apache.http.params.CoreProtocolPNames; |
46 |
|
import org.apache.http.params.HttpConnectionParams; |
47 |
|
import org.apache.http.params.HttpParams; |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
@author |
53 |
|
|
54 |
|
|
|
|
| 0% |
Uncovered Elements: 63 (63) |
Complexity: 10 |
Complexity Density: 0.22 |
|
55 |
|
public class HttpClientUtils |
56 |
|
{ |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
@param |
62 |
|
@param |
63 |
|
@return |
64 |
|
@throws |
65 |
|
@throws |
66 |
|
@throws |
67 |
|
|
|
|
| 0% |
Uncovered Elements: 22 (22) |
Complexity: 4 |
Complexity Density: 0.25 |
|
68 |
0 |
public static BufferedReader doHttpUrlPost(String postUrl,... |
69 |
|
List<NameValuePair> vals, int connectionTimeoutMs, |
70 |
|
int readTimeoutMs) throws ClientProtocolException, IOException |
71 |
|
{ |
72 |
|
|
73 |
0 |
HttpParams params = new BasicHttpParams(); |
74 |
0 |
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, |
75 |
|
HttpVersion.HTTP_1_1); |
76 |
0 |
if (connectionTimeoutMs > 0) |
77 |
|
{ |
78 |
0 |
HttpConnectionParams.setConnectionTimeout(params, |
79 |
|
connectionTimeoutMs); |
80 |
|
} |
81 |
0 |
if (readTimeoutMs > 0) |
82 |
|
{ |
83 |
0 |
HttpConnectionParams.setSoTimeout(params, readTimeoutMs); |
84 |
|
} |
85 |
0 |
HttpClient httpclient = new DefaultHttpClient(params); |
86 |
0 |
HttpPost httppost = new HttpPost(postUrl); |
87 |
0 |
UrlEncodedFormEntity ue = new UrlEncodedFormEntity(vals, "UTF-8"); |
88 |
0 |
httppost.setEntity(ue); |
89 |
0 |
HttpResponse response = httpclient.execute(httppost); |
90 |
0 |
HttpEntity resEntity = response.getEntity(); |
91 |
|
|
92 |
0 |
if (resEntity != null) |
93 |
|
{ |
94 |
0 |
BufferedReader r = new BufferedReader( |
95 |
|
new InputStreamReader(resEntity.getContent())); |
96 |
0 |
return r; |
97 |
|
} |
98 |
|
else |
99 |
|
{ |
100 |
0 |
return null; |
101 |
|
} |
102 |
|
} |
103 |
|
|
|
|
| 0% |
Uncovered Elements: 19 (19) |
Complexity: 3 |
Complexity Density: 0.2 |
|
104 |
0 |
public static BufferedReader doHttpMpartFilePost(String postUrl,... |
105 |
|
List<NameValuePair> vals, String fparm, File file, String mtype) |
106 |
|
throws ClientProtocolException, IOException |
107 |
|
{ |
108 |
0 |
HttpClient httpclient = new DefaultHttpClient(); |
109 |
0 |
HttpPost httppost = new HttpPost(postUrl); |
110 |
0 |
MultipartEntity mpe = new MultipartEntity( |
111 |
|
HttpMultipartMode.BROWSER_COMPATIBLE); |
112 |
0 |
for (NameValuePair nvp : vals) |
113 |
|
{ |
114 |
0 |
mpe.addPart(nvp.getName(), new StringBody(nvp.getValue())); |
115 |
|
} |
116 |
|
|
117 |
0 |
FileBody fb = new FileBody(file, |
118 |
0 |
mtype != null ? mtype : "application/octet-stream"); |
119 |
0 |
mpe.addPart(fparm, fb); |
120 |
0 |
UrlEncodedFormEntity ue = new UrlEncodedFormEntity(vals, "UTF-8"); |
121 |
0 |
httppost.setEntity(ue); |
122 |
0 |
HttpResponse response = httpclient.execute(httppost); |
123 |
0 |
HttpEntity resEntity = response.getEntity(); |
124 |
|
|
125 |
0 |
if (resEntity != null) |
126 |
|
{ |
127 |
0 |
BufferedReader r = new BufferedReader( |
128 |
|
new InputStreamReader(resEntity.getContent())); |
129 |
0 |
return r; |
130 |
|
} |
131 |
|
else |
132 |
|
{ |
133 |
0 |
return null; |
134 |
|
} |
135 |
|
} |
136 |
|
|
|
|
| 0% |
Uncovered Elements: 19 (19) |
Complexity: 3 |
Complexity Density: 0.2 |
|
137 |
0 |
public static BufferedReader doHttpMpartInputstreamPost(String postUrl,... |
138 |
|
List<NameValuePair> vals, String fparm, String fname, |
139 |
|
InputStream is, String mtype) |
140 |
|
throws ClientProtocolException, IOException |
141 |
|
{ |
142 |
0 |
HttpClient httpclient = new DefaultHttpClient(); |
143 |
0 |
HttpPost httppost = new HttpPost(postUrl); |
144 |
0 |
MultipartEntity mpe = new MultipartEntity(HttpMultipartMode.STRICT); |
145 |
0 |
for (NameValuePair nvp : vals) |
146 |
|
{ |
147 |
0 |
mpe.addPart(nvp.getName(), new StringBody(nvp.getValue())); |
148 |
|
} |
149 |
|
|
150 |
0 |
InputStreamBody fb = (mtype != null) |
151 |
|
? new InputStreamBody(is, fname, mtype) |
152 |
|
: new InputStreamBody(is, fname); |
153 |
0 |
mpe.addPart(fparm, fb); |
154 |
0 |
UrlEncodedFormEntity ue = new UrlEncodedFormEntity(vals, "UTF-8"); |
155 |
0 |
httppost.setEntity(ue); |
156 |
0 |
HttpResponse response = httpclient.execute(httppost); |
157 |
0 |
HttpEntity resEntity = response.getEntity(); |
158 |
|
|
159 |
0 |
if (resEntity != null) |
160 |
|
{ |
161 |
0 |
BufferedReader r = new BufferedReader( |
162 |
|
new InputStreamReader(resEntity.getContent())); |
163 |
0 |
return r; |
164 |
|
} |
165 |
|
else |
166 |
|
{ |
167 |
0 |
return null; |
168 |
|
} |
169 |
|
} |
170 |
|
} |