1
1
package http .requests ;
2
2
3
+ import java .util .ArrayList ;
4
+ import java .util .Iterator ;
5
+
3
6
import org .apache .http .Header ;
4
7
import org .apache .http .HttpEntity ;
5
8
import org .apache .http .HttpResponse ;
9
+ import org .apache .http .NameValuePair ;
6
10
import org .apache .http .auth .UsernamePasswordCredentials ;
7
11
import org .apache .http .client .methods .HttpGet ;
8
12
import org .apache .http .impl .auth .BasicScheme ;
9
13
import org .apache .http .impl .client .DefaultHttpClient ;
14
+ import org .apache .http .message .BasicNameValuePair ;
10
15
import org .apache .http .util .EntityUtils ;
11
16
12
17
public class GetRequest
@@ -15,17 +20,28 @@ public class GetRequest
15
20
String content ;
16
21
HttpResponse response ;
17
22
UsernamePasswordCredentials creds ;
23
+ ArrayList <BasicNameValuePair > headerPairs ;
18
24
25
+
19
26
public GetRequest (String url )
20
27
{
21
28
this .url = url ;
29
+ headerPairs = new ArrayList <BasicNameValuePair >();
30
+
22
31
}
23
32
24
33
public void addUser (String user , String pwd )
25
34
{
26
35
creds = new UsernamePasswordCredentials (user , pwd );
27
- }
28
-
36
+
37
+ }
38
+
39
+ public void addHeader (String key ,String value ) {
40
+ BasicNameValuePair nvp = new BasicNameValuePair (key ,value );
41
+ headerPairs .add (nvp );
42
+
43
+ }
44
+
29
45
public void send ()
30
46
{
31
47
try {
@@ -37,6 +53,13 @@ public void send()
37
53
httpGet .addHeader (new BasicScheme ().authenticate (creds , httpGet , null ));
38
54
}
39
55
56
+ Iterator <BasicNameValuePair > headerIterator = headerPairs .iterator ();
57
+ while (headerIterator .hasNext ()) {
58
+ BasicNameValuePair headerPair = headerIterator .next ();
59
+ httpGet .addHeader (headerPair .getName (),headerPair .getValue ());
60
+ }
61
+
62
+
40
63
response = httpClient .execute ( httpGet );
41
64
HttpEntity entity = response .getEntity ();
42
65
this .content = EntityUtils .toString (response .getEntity ());
0 commit comments