24
24
25
25
public class PostRequest
26
26
{
27
- String url ;
28
- ArrayList <BasicNameValuePair > nameValuePairs ;
29
- StringEntity stringEntity ;
30
- HashMap <String ,File > nameFilePairs ;
31
- ArrayList <BasicNameValuePair > headerPairs ;
32
-
33
-
34
- String content ;
35
- String encoding ;
36
- HttpResponse response ;
37
- UsernamePasswordCredentials creds ;
38
-
39
- public PostRequest (String url )
40
- {
41
- this (url , "ISO-8859-1" );
42
- }
43
-
44
- public PostRequest (String url , String encoding )
45
- {
46
- this .url = url ;
47
- this .encoding = encoding ;
48
- nameValuePairs = new ArrayList <BasicNameValuePair >();
49
- nameFilePairs = new HashMap <String ,File >();
50
- this .headerPairs = new ArrayList <BasicNameValuePair >();
51
- }
52
-
53
- public void addUser (String user , String pwd )
54
- {
55
- creds = new UsernamePasswordCredentials (user , pwd );
56
- }
57
-
58
- public void addHeader (String key ,String value ) {
59
- BasicNameValuePair nvp = new BasicNameValuePair (key ,value );
60
- headerPairs .add (nvp );
61
-
62
- }
63
-
64
- public void addData (String key , String value )
65
- {
66
- BasicNameValuePair nvp = new BasicNameValuePair (key ,value );
67
- nameValuePairs .add (nvp );
68
- }
69
-
70
- // overloads addData so you can add a JSON string:
27
+ String url ;
28
+ ArrayList <BasicNameValuePair > nameValuePairs ;
29
+ StringEntity stringEntity ;
30
+ HashMap <String , File > nameFilePairs ;
31
+ ArrayList <BasicNameValuePair > headerPairs ;
32
+
33
+
34
+ String content ;
35
+ String encoding ;
36
+ HttpResponse response ;
37
+ UsernamePasswordCredentials creds ;
38
+
39
+ public PostRequest (String url )
40
+ {
41
+ this (url , "ISO-8859-1" );
42
+ }
43
+
44
+ public PostRequest (String url , String encoding )
45
+ {
46
+ this .url = url ;
47
+ this .encoding = encoding ;
48
+ nameValuePairs = new ArrayList <BasicNameValuePair >();
49
+ nameFilePairs = new HashMap <String , File >();
50
+ this .headerPairs = new ArrayList <BasicNameValuePair >();
51
+ }
52
+
53
+ public void addUser (String user , String pwd )
54
+ {
55
+ creds = new UsernamePasswordCredentials (user , pwd );
56
+ }
57
+
58
+ public void addHeader (String key , String value ) {
59
+ BasicNameValuePair nvp = new BasicNameValuePair (key , value );
60
+ headerPairs .add (nvp );
61
+ }
62
+
63
+ public void addData (String key , String value )
64
+ {
65
+ BasicNameValuePair nvp = new BasicNameValuePair (key , value );
66
+ nameValuePairs .add (nvp );
67
+ }
68
+
69
+ // overloads addData so you can add a JSON string:
71
70
public void addData (String json )
72
71
{
73
- try {
74
- stringEntity = new StringEntity (json );
75
- } catch ( Exception e ) {
76
- e .printStackTrace ();
72
+ try {
73
+ stringEntity = new StringEntity (json );
74
+ }
75
+ catch ( Exception e ) {
76
+ e .printStackTrace ();
77
77
}
78
78
}
79
79
80
- public void addFile (String name , File f ) {
81
- nameFilePairs .put (name ,f );
82
- }
83
-
84
- public void addFile (String name , String path ) {
85
- File f = new File (path );
86
- nameFilePairs .put (name ,f );
87
- }
88
-
89
- public void send ()
90
- {
91
- try {
92
- DefaultHttpClient httpClient = new DefaultHttpClient ();
93
- HttpPost httpPost = new HttpPost (url );
94
-
95
- if (creds != null ){
96
- httpPost .addHeader (new BasicScheme ().authenticate (creds , httpPost , null ));
97
- }
98
-
99
- if (nameFilePairs .isEmpty ()) {
100
- httpPost .setEntity (new UrlEncodedFormEntity (nameValuePairs , encoding ));
101
- } else {
102
- MultipartEntity mentity = new MultipartEntity ();
103
- Iterator <Entry <String ,File >> it = nameFilePairs .entrySet ().iterator ();
104
- while (it .hasNext ()) {
105
- Entry <String , File > pair = it .next ();
106
- String name = (String ) pair .getKey ();
107
- File f = (File ) pair .getValue ();
108
- mentity .addPart (name , new FileBody (f ));
109
- }
110
- for (NameValuePair nvp : nameValuePairs ) {
111
- mentity .addPart (nvp .getName (), new StringBody (nvp .getValue ()));
112
- }
113
- httpPost .setEntity (mentity );
114
- }
115
-
116
- if (stringEntity != null ) {
117
- httpPost .setEntity (stringEntity );
118
- }
119
-
120
- Iterator <BasicNameValuePair > headerIterator = headerPairs .iterator ();
121
- while (headerIterator .hasNext ()) {
122
- BasicNameValuePair headerPair = headerIterator .next ();
123
- httpPost .addHeader (headerPair .getName (),headerPair .getValue ());
124
- }
125
-
126
- response = httpClient .execute ( httpPost );
127
- HttpEntity entity = response .getEntity ();
128
- this .content = EntityUtils .toString (response .getEntity ());
129
-
130
- if ( entity != null ) EntityUtils .consume (entity );
131
-
132
- httpClient .getConnectionManager ().shutdown ();
133
-
134
- // Clear it out for the next time
135
- nameValuePairs .clear ();
136
- nameFilePairs .clear ();
137
- headerPairs .clear ();
138
-
139
- } catch ( Exception e ) {
140
- e .printStackTrace ();
141
- }
142
- }
143
-
144
- /* Getters
145
- _____________________________________________________________ */
146
-
147
- public String getContent ()
148
- {
149
- return this .content ;
150
- }
151
-
152
- public String getHeader (String name )
153
- {
154
- Header header = response .getFirstHeader (name );
155
- if (header == null )
156
- {
157
- return "" ;
158
- }
159
- else
160
- {
161
- return header .getValue ();
162
- }
163
- }
164
- }
80
+ public void addFile (String name , File f ) {
81
+ nameFilePairs .put (name , f );
82
+ }
83
+
84
+ public void addFile (String name , String path ) {
85
+ File f = new File (path );
86
+ nameFilePairs .put (name , f );
87
+ }
88
+
89
+ public void send ()
90
+ {
91
+ try {
92
+ DefaultHttpClient httpClient = new DefaultHttpClient ();
93
+ HttpPost httpPost = new HttpPost (url );
94
+
95
+ if (creds != null ) {
96
+ httpPost .addHeader (new BasicScheme ().authenticate (creds , httpPost , null ));
97
+ }
98
+
99
+ if (nameFilePairs .isEmpty ()) {
100
+ httpPost .setEntity (new UrlEncodedFormEntity (nameValuePairs , encoding ));
101
+ } else {
102
+ MultipartEntity mentity = new MultipartEntity ();
103
+ Iterator <Entry <String , File >> it = nameFilePairs .entrySet ().iterator ();
104
+ while (it .hasNext ()) {
105
+ Entry <String , File > pair = it .next ();
106
+ String name = (String ) pair .getKey ();
107
+ File f = (File ) pair .getValue ();
108
+ mentity .addPart (name , new FileBody (f ));
109
+ }
110
+ for (NameValuePair nvp : nameValuePairs ) {
111
+ mentity .addPart (nvp .getName (), new StringBody (nvp .getValue ()));
112
+ }
113
+ httpPost .setEntity (mentity );
114
+ }
115
+
116
+ if (stringEntity != null ) {
117
+ httpPost .setEntity (stringEntity );
118
+ }
119
+
120
+ Iterator <BasicNameValuePair > headerIterator = headerPairs .iterator ();
121
+ while (headerIterator .hasNext ()) {
122
+ BasicNameValuePair headerPair = headerIterator .next ();
123
+ httpPost .addHeader (headerPair .getName (), headerPair .getValue ());
124
+ }
125
+
126
+ response = httpClient .execute ( httpPost );
127
+ HttpEntity entity = response .getEntity ();
128
+ this .content = EntityUtils .toString (response .getEntity ());
129
+
130
+ if ( entity != null ) EntityUtils .consume (entity );
131
+
132
+ httpClient .getConnectionManager ().shutdown ();
133
+
134
+ // Clear it out for the next time
135
+ nameValuePairs .clear ();
136
+ nameFilePairs .clear ();
137
+ headerPairs .clear ();
138
+ }
139
+ catch ( Exception e ) {
140
+ e .printStackTrace ();
141
+ }
142
+ }
143
+
144
+ /* Getters
145
+ _____________________________________________________________ */
146
+
147
+ public String getContent ()
148
+ {
149
+ return this .content ;
150
+ }
151
+
152
+ public String getHeader (String name )
153
+ {
154
+ Header header = response .getFirstHeader (name );
155
+ if (header == null )
156
+ {
157
+ return "" ;
158
+ } else
159
+ {
160
+ return header .getValue ();
161
+ }
162
+ }
163
+ }
0 commit comments