Subversion Repositories javautils

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 daniel-mar 1
package de.viathinksoft.utils.http;
2
 
3
// Needs Apache Client 4.x
4
// http://hc.apache.org/downloads.cgi
5
 
6
import java.io.File;
7
import java.io.FileOutputStream;
8
import java.io.IOException;
9
import java.io.InputStream;
10
import java.io.OutputStream;
11
import java.net.URLEncoder;
12
import java.util.List;
13
 
14
import org.apache.http.HttpResponse;
15
import org.apache.http.HttpVersion;
16
import org.apache.http.NameValuePair;
17
import org.apache.http.client.ClientProtocolException;
18
import org.apache.http.client.entity.UrlEncodedFormEntity;
19
import org.apache.http.client.methods.HttpGet;
20
import org.apache.http.client.methods.HttpPost;
21
import org.apache.http.cookie.Cookie;
22
import org.apache.http.impl.client.DefaultHttpClient;
23
import org.apache.http.params.BasicHttpParams;
24
import org.apache.http.params.HttpParams;
25
import org.apache.http.params.HttpProtocolParams;
26
import org.apache.http.protocol.HTTP;
27
 
28
public class HttpUtils {
29
 
30
        private DefaultHttpClient httpClient;
31
 
32
        public HttpUtils() {
33
                httpClient = new DefaultHttpClient();
34
        }
35
 
36
        public HttpUtils(String userAgent) {
37
                this();
38
 
39
                HttpParams params = new BasicHttpParams();
40
                HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
41
                HttpProtocolParams.setContentCharset(params, "UTF-8");
42
                HttpProtocolParams.setUserAgent(params, userAgent);
43
                HttpProtocolParams.setUseExpectContinue(params, true);
44
 
45
                httpClient.setParams(params);
46
        }
47
 
48
        public DefaultHttpClient getHttpClient() {
49
                return httpClient;
50
        }
51
 
52
        public List<Cookie> getAllCookies() {
53
                return httpClient.getCookieStore().getCookies();
54
        }
55
 
56
        public void clearAllCookies() {
57
                httpClient.getCookieStore().clear();
58
        }
59
 
60
        public MyHttpResponse doGet(String url) throws ClientProtocolException,
61
                        IOException {
62
                HttpGet httpget = new HttpGet(url);
63
                HttpResponse response = httpClient.execute(httpget);
64
                return new MyHttpResponse(response);
65
        }
66
 
67
        public MyHttpResponse doGet(String url, NameValuePairArray parameters,
68
                        NameValuePairArray requestHeaders) throws ClientProtocolException,
69
                        IOException {
70
                String myurl = url.concat("?");
71
 
72
                for (NameValuePair p : parameters) {
73
                        myurl = myurl.concat(p.getName()).concat("=").concat(
74
                                        URLEncoder.encode(p.getValue(), "UTF-8")).concat("&");
75
                }
76
 
77
                HttpGet httpget = new HttpGet(myurl);
78
 
79
                for (NameValuePair rh : requestHeaders) {
80
                        httpget.addHeader(rh.getName(), rh.getValue());
81
                }
82
 
83
                HttpResponse response = httpClient.execute(httpget);
84
                return new MyHttpResponse(response);
85
        }
86
 
87
        public MyHttpResponse doGet(String url, NameValuePairArray parameters)
88
                        throws ClientProtocolException, IOException {
89
                return doGet(url, parameters, new NameValuePairArray());
90
        }
91
 
92
        public MyHttpResponse doSimplePost(String url, NameValuePairArray postData,
93
                        NameValuePairArray requestHeaders) throws ClientProtocolException,
94
                        IOException {
95
                HttpPost httppost = new HttpPost(url);
96
 
97
                httppost.setEntity(new UrlEncodedFormEntity(postData, HTTP.UTF_8));
98
 
99
                for (NameValuePair rh : requestHeaders) {
100
                        httppost.addHeader(rh.getName(), rh.getValue());
101
                }
102
 
103
                HttpResponse response = httpClient.execute(httppost);
104
                return new MyHttpResponse(response);
105
        }
106
 
107
        public MyHttpResponse doSimplePost(String url, NameValuePairArray postData)
108
                        throws ClientProtocolException, IOException {
109
                return doSimplePost(url, postData, new NameValuePairArray());
110
        }
111
 
112
        public MyHttpResponse doMultiPartPost(String url,
113
                        MultipartPostData postData, NameValuePairArray requestHeaders)
114
                        throws ClientProtocolException, IOException {
115
                DefaultHttpClient httpclient = new DefaultHttpClient();
116
 
117
                HttpPost httppost = new HttpPost(url);
118
                httppost.setEntity(postData);
119
 
120
                for (NameValuePair rh : requestHeaders) {
121
                        httppost.addHeader(rh.getName(), rh.getValue());
122
                }
123
 
124
                HttpResponse response = httpclient.execute(httppost);
125
                return new MyHttpResponse(response);
126
        }
127
 
128
        public MyHttpResponse doMultiPartPost(String url, MultipartPostData postData)
129
                        throws ClientProtocolException, IOException {
130
                return doMultiPartPost(url, postData, new NameValuePairArray());
131
        }
132
 
133
        protected void writeInputStreamToFile(InputStream inputStream, File outFile)
134
                        throws IOException {
135
                OutputStream out = new FileOutputStream(outFile);
136
                byte buf[] = new byte[1024];
137
                int len;
138
                while ((len = inputStream.read(buf)) > 0) {
139
                        out.write(buf, 0, len);
140
                }
141
                out.close();
142
                inputStream.close();
143
        }
144
 
145
        protected void writeResponseToFile(HttpResponse response, File outputFile)
146
                        throws IllegalStateException, IOException {
147
                File tmp = new File("~~download.tmp");
148
                writeInputStreamToFile(response.getEntity().getContent(), tmp);
149
                if (outputFile.exists()) {
150
                        if (!outputFile.delete()) {
151
                                tmp.delete();
152
                                throw new IOException("Destination file already exists and could not be deleted.");
153
                        }
154
                }
155
 
156
                if (!tmp.renameTo(outputFile)) {
157
                        tmp.delete();
158
                        throw new IOException("File could not moved to destination! Does the destination directory with accurate permissions exist?");
159
                }
160
        }
161
 
162
        public void downloadFile(String url, File outputFile,
163
                        NameValuePairArray requestHeaders) throws ClientProtocolException,
164
                        IOException {
165
                HttpGet httpget = new HttpGet(url);
166
 
167
                for (NameValuePair rh : requestHeaders) {
168
                        httpget.addHeader(rh.getName(), rh.getValue());
169
                }
170
 
171
                HttpResponse response = httpClient.execute(httpget);
172
                writeResponseToFile(response, outputFile);
173
        }
174
 
175
        public void downloadFile(String url, File outputFile)
176
                        throws ClientProtocolException, IOException {
177
                downloadFile(url, outputFile, new NameValuePairArray());
178
        }
179
}