Version of of this class had some bugs so I corrected them and here is the latest version 🙂 Enjoy!
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
package moz.http; import java.net.*; import java.io.*; import java.util.ArrayList; import java.util.Enumeration; import java.util.Hashtable; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.Map.Entry; import org.apache.http.client.HttpClient; import org.apache.commons.*; import android.util.Log; /** * HTTP Request class * * You can use this class and distribute it as long as you give proper credit * and place and leave this notice intact :). Check my blog for updated * version(s) of this class (http://moazzam-khan.com) * * Usage Examples: * * Get Request * -------------------------------- * HttpData data = HttpRequest.get("http://example.com/index.php?user=hello"); * System.out.println(data.content); * * Post Request * -------------------------------- * HttpData data = HttpRequest.post("http://xyz.com", "var1=val&var2=val2"); * System.out.println(data.content); * Enumeration<String> keys = dat.cookies.keys(); // cookies * while (keys.hasMoreElements()) { * System.out.println(keys.nextElement() + " = " + * data.cookies.get(keys.nextElement() + "rn"); * } * Enumeration<String> keys = dat.headers.keys(); // headers * while (keys.hasMoreElements()) { * System.out.println(keys.nextElement() + " = " + * data.headers.get(keys.nextElement() + "rn"); * } * * Upload a file * -------------------------------- * ArrayList<File> files = new ArrayList(); * files.add(new File("/etc/someFile")); * files.add(new File("/home/user/anotherFile")); * * Hashtable<String, String> ht = new Hashtable<String, String>(); * ht.put("var1", "val1"); * * HttpData data = HttpRequest.post("http://xyz.com", ht, files); * System.out.println(data.content); * * @author Moazzam Khan */ public class HttpRequest { /** * HttpGet request * * @param sUrl * @return */ public static HttpData get(String sUrl) { HttpData ret = new HttpData(); String str; StringBuffer buff = new StringBuffer(); try { URL url = new URL(sUrl); URLConnection con = url.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); while ((str = in.readLine()) != null) { buff.append(str); } ret.content = buff.toString(); //get headers Map<String, List<String>> headers = con.getHeaderFields(); Set<Entry<String, List<String>>> hKeys = headers.entrySet(); for (Iterator<Entry<String, List<String>>> i = hKeys.iterator(); i.hasNext();) { Entry<String, List<String>> m = i.next(); Log.w("HEADER_KEY", m.getKey() + ""); ret.headers.put(m.getKey(), m.getValue().toString()); if (m.getKey().equals("set-cookie")) ret.cookies.put(m.getKey(), m.getValue().toString()); } } catch (Exception e) { Log.e("HttpRequest", e.toString()); } return ret; } /** * HTTP post request * * @param sUrl * @param ht * @return * @throws Exception */ public static HttpData post(String sUrl, Hashtable<String, String> ht) throws Exception { String key; StringBuffer data = new StringBuffer(); Enumeration<String> keys = ht.keys(); while (keys.hasMoreElements()) { key = keys.nextElement(); data.append(URLEncoder.encode(key, "UTF-8")); data.append("="); data.append(URLEncoder.encode(ht.get(key), "UTF-8")); data.append("&"); } return HttpRequest.post(sUrl, data.toString()); } /** * HTTP post request * * @param sUrl * @param data * @return */ public static HttpData post(String sUrl, String data) { StringBuffer ret = new StringBuffer(); HttpData dat = new HttpData(); String header; try { // Send data URL url = new URL(sUrl); URLConnection conn = url.openConnection(); conn.setDoOutput(true); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data); wr.flush(); // Get the response Map<String, List<String>> headers = conn.getHeaderFields(); Set<Entry<String, List<String>>> hKeys = headers.entrySet(); for (Iterator<Entry<String, List<String>>> i = hKeys.iterator(); i.hasNext();) { Entry<String, List<String>> m = i.next(); Log.w("HEADER_KEY", m.getKey() + ""); dat.headers.put(m.getKey(), m.getValue().toString()); if (m.getKey().equals("set-cookie")) dat.cookies.put(m.getKey(), m.getValue().toString()); } BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = rd.readLine()) != null) { ret.append(line); } wr.close(); rd.close(); } catch (Exception e) { Log.e("ERROR", "ERROR IN CODE:"+e.getMessage()); } dat.content = ret.toString(); return dat; } /** * Post request (upload files) * @param sUrl * @param files * @return HttpData */ public static HttpData post(String sUrl, ArrayList<File> files) { Hashtable<String, String> ht = new Hashtable<String, String>(); return HttpRequest.post(sUrl, ht, files); } /** * Post request (upload files) * @param sUrl * @param params Form data * @param files * @return */ public static HttpData post(String sUrl, Hashtable<String, String> params, ArrayList<File> files) { HttpData ret = new HttpData(); try { String boundary = "*****************************************"; String newLine = "rn"; int bytesAvailable; int bufferSize; int maxBufferSize = 4096; int bytesRead; URL url = new URL(sUrl); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setDoInput(true); con.setDoOutput(true); con.setUseCaches(false); con.setRequestMethod("POST"); con.setRequestProperty("Connection", "Keep-Alive"); con.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); DataOutputStream dos = new DataOutputStream(con.getOutputStream()); //dos.writeChars(params); //upload files for (int i=0; i<files.size(); i++) { Log.i("HREQ", i+""); FileInputStream fis = new FileInputStream(files.get(i)); dos.writeBytes("--" + boundary + newLine); dos.writeBytes("Content-Disposition: form-data; " + "name="file_"+i+"";filename="" + files.get(i).getPath() +""" + newLine + newLine); bytesAvailable = fis.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); byte[] buffer = new byte[bufferSize]; bytesRead = fis.read(buffer, 0, bufferSize); while (bytesRead > 0) { dos.write(buffer, 0, bufferSize); bytesAvailable = fis.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); bytesRead = fis.read(buffer, 0, bufferSize); } dos.writeBytes(newLine); dos.writeBytes("--" + boundary + "--" + newLine); fis.close(); } // Now write the data Enumeration keys = params.keys(); String key, val; while (keys.hasMoreElements()) { key = keys.nextElement().toString(); val = params.get(key); dos.writeBytes("--" + boundary + newLine); dos.writeBytes("Content-Disposition: form-data;name="" + key+""" + newLine + newLine + val); dos.writeBytes(newLine); dos.writeBytes("--" + boundary + "--" + newLine); } dos.flush(); BufferedReader rd = new BufferedReader( new InputStreamReader(con.getInputStream())); String line; while ((line = rd.readLine()) != null) { ret.content += line + "rn"; } //get headers Map<String, List<String>> headers = con.getHeaderFields(); Set<Entry<String, List<String>>> hKeys = headers.entrySet(); for (Iterator<Entry<String, List<String>>> i = hKeys.iterator(); i.hasNext();) { Entry<String, List<String>> m = i.next(); Log.w("HEADER_KEY", m.getKey() + ""); ret.headers.put(m.getKey(), m.getValue().toString()); if (m.getKey().equals("set-cookie")) ret.cookies.put(m.getKey(), m.getValue().toString()); } dos.close(); rd.close(); } catch (MalformedURLException me) { } catch (IOException ie) { } catch (Exception e) { Log.e("HREQ", "Exception: "+e.toString()); } return ret; } } |
You will also need the class below:
1 2 3 4 5 6 7 |
package moz.http; import java.util.Hashtable; public class HttpData { public String content; public Hashtable cookies = new Hashtable(); public Hashtable headers = new Hashtable(); } |