├── .classpath
├── .project
├── AndroidManifest.xml
├── default.properties
├── gen
└── org
│ └── addhen
│ └── whereami
│ └── R.java
├── res
├── drawable
│ └── icon.png
├── layout
│ └── main.xml
└── values
│ └── strings.xml
└── src
└── org
└── addhen
└── whereami
├── LocationService.java
├── SMSReceiver.java
├── Whereami.java
└── net
└── ClientHttpRequest.java
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
Title: Client HTTP Request class
17 | *Description: this class helps to send POST HTTP requests with various form data, 18 | * including files. Cookies can be added to be included in the request.
19 | * 20 | * @author Vlad Patryshev 21 | * @version 1.0 22 | */ 23 | public class ClientHttpRequest { 24 | URLConnection connection; 25 | OutputStream os = null; 26 | 27 | @SuppressWarnings("unchecked") 28 | Map cookies = new HashMap(); 29 | 30 | protected void connect() throws IOException { 31 | if (os == null) os = connection.getOutputStream(); 32 | } 33 | 34 | protected void write(char c) throws IOException { 35 | connect(); 36 | os.write(c); 37 | } 38 | 39 | protected void write(String s) throws IOException { 40 | connect(); 41 | os.write(s.getBytes()); 42 | } 43 | 44 | protected void newline() throws IOException { 45 | connect(); 46 | write("\r\n"); 47 | } 48 | 49 | protected void writeln(String s) throws IOException { 50 | connect(); 51 | write(s); 52 | newline(); 53 | } 54 | 55 | private static Random random = new Random(); 56 | 57 | protected static String randomString() { 58 | return Long.toString(random.nextLong(), 36); 59 | } 60 | 61 | String boundary = "---------------------------" + randomString() + randomString() + randomString(); 62 | 63 | private void boundary() throws IOException { 64 | write("--"); 65 | write(boundary); 66 | } 67 | 68 | /** 69 | * Creates a new multipart POST HTTP request on a freshly opened URLConnection 70 | * 71 | * @param connection an already open URL connection 72 | * @throws IOException 73 | */ 74 | public ClientHttpRequest(URLConnection connection) throws IOException { 75 | this.connection = connection; 76 | connection.setDoOutput(true); 77 | connection.setRequestProperty("Content-Type", 78 | "multipart/form-data; boundary=" + boundary); 79 | } 80 | 81 | /** 82 | * Creates a new multipart POST HTTP request for a specified URL 83 | * 84 | * @param url the URL to send request to 85 | * @throws IOException 86 | */ 87 | public ClientHttpRequest(URL url) throws IOException { 88 | this(url.openConnection()); 89 | } 90 | 91 | /** 92 | * Creates a new multipart POST HTTP request for a specified URL string 93 | * 94 | * @param urlString the string representation of the URL to send request to 95 | * @throws IOException 96 | */ 97 | public ClientHttpRequest(String urlString) throws IOException { 98 | this(new URL(urlString)); 99 | } 100 | 101 | 102 | @SuppressWarnings({ "unchecked", "unused" }) 103 | private void postCookies() { 104 | StringBuffer cookieList = new StringBuffer(); 105 | 106 | for (Iterator i = cookies.entrySet().iterator(); i.hasNext();) { 107 | Map.Entry entry = (Map.Entry)(i.next()); 108 | cookieList.append(entry.getKey().toString() + "=" + entry.getValue()); 109 | 110 | if (i.hasNext()) { 111 | cookieList.append("; "); 112 | } 113 | } 114 | if (cookieList.length() > 0) { 115 | connection.setRequestProperty("Cookie", cookieList.toString()); 116 | } 117 | } 118 | 119 | /** 120 | * adds a cookie to the requst 121 | * @param name cookie name 122 | * @param value cookie value 123 | * @throws IOException 124 | */ 125 | @SuppressWarnings("unchecked") 126 | public void setCookie(String name, String value) throws IOException { 127 | cookies.put(name, value); 128 | } 129 | 130 | /** 131 | * adds cookies to the request 132 | * @param cookies the cookie "name-to-value" map 133 | * @throws IOException 134 | */ 135 | @SuppressWarnings("unchecked") 136 | public void setCookies(Map cookies) throws IOException { 137 | if (cookies == null) return; 138 | this.cookies.putAll(cookies); 139 | } 140 | 141 | /** 142 | * adds cookies to the request 143 | * @param cookies array of cookie names and values (cookies[2*i] is a name, cookies[2*i + 1] is a value) 144 | * @throws IOException 145 | */ 146 | public void setCookies(String[] cookies) throws IOException { 147 | if (cookies == null) return; 148 | for (int i = 0; i < cookies.length - 1; i+=2) { 149 | setCookie(cookies[i], cookies[i+1]); 150 | } 151 | } 152 | 153 | private void writeName(String name) throws IOException { 154 | newline(); 155 | write("Content-Disposition: form-data; name=\""); 156 | write(name); 157 | write('"'); 158 | } 159 | 160 | /** 161 | * adds a string parameter to the request 162 | * @param name parameter name 163 | * @param value parameter value 164 | * @throws IOException 165 | */ 166 | public void setParameter(String name, String value) throws IOException { 167 | boundary(); 168 | writeName(name); 169 | newline(); newline(); 170 | writeln(value); 171 | } 172 | 173 | private static void pipe(InputStream in, OutputStream out) throws IOException { 174 | byte[] buf = new byte[500000]; 175 | int nread; 176 | @SuppressWarnings("unused") 177 | int navailable; 178 | int total = 0; 179 | synchronized (in) { 180 | while((nread = in.read(buf, 0, buf.length)) >= 0) { 181 | out.write(buf, 0, nread); 182 | total += nread; 183 | } 184 | } 185 | out.flush(); 186 | buf = null; 187 | } 188 | 189 | /** 190 | * adds a file parameter to the request 191 | * @param name parameter name 192 | * @param filename the name of the file 193 | * @param is input stream to read the contents of the file from 194 | * @throws IOException 195 | */ 196 | @SuppressWarnings("static-access") 197 | public void setParameter(String name, String filename, InputStream is) throws IOException { 198 | boundary(); 199 | writeName(name); 200 | write("; filename=\""); 201 | write(filename); 202 | write('"'); 203 | newline(); 204 | write("Content-Type: "); 205 | String type = connection.guessContentTypeFromName(filename); 206 | if (type == null) type = "application/octet-stream"; 207 | writeln(type); 208 | newline(); 209 | pipe(is, os); 210 | newline(); 211 | } 212 | 213 | /** 214 | * adds a file parameter to the request 215 | * @param name parameter name 216 | * @param file the file to upload 217 | * @throws IOException 218 | */ 219 | public void setParameter(String name, File file) throws IOException { 220 | setParameter(name, file.getPath(), new FileInputStream(file)); 221 | } 222 | 223 | /** 224 | * adds a parameter to the request; if the parameter is a File, the file is uploaded, otherwise the string value of the parameter is passed in the request 225 | * @param name parameter name 226 | * @param object parameter value, a File or anything else that can be stringified 227 | * @throws IOException 228 | */ 229 | public void setParameter(String name, Object object) throws IOException { 230 | if (object instanceof File) { 231 | setParameter(name, (File) object); 232 | } else { 233 | setParameter(name, object.toString()); 234 | } 235 | } 236 | 237 | /** 238 | * adds parameters to the request 239 | * @param parameters "name-to-value" map of parameters; if a value is a file, the file is uploaded, otherwise it is stringified and sent in the request 240 | * @throws IOException 241 | */ 242 | @SuppressWarnings("unchecked") 243 | public void setParameters(Map parameters) throws IOException { 244 | if (parameters == null) return; 245 | for (Iterator i = parameters.entrySet().iterator(); i.hasNext();) { 246 | Map.Entry entry = (Map.Entry)i.next(); 247 | setParameter(entry.getKey().toString(), entry.getValue()); 248 | } 249 | } 250 | 251 | /** 252 | * adds parameters to the request 253 | * @param parameters array of parameter names and values (parameters[2*i] is a name, parameters[2*i + 1] is a value); if a value is a file, the file is uploaded, otherwise it is stringified and sent in the request 254 | * @throws IOException 255 | */ 256 | public void setParameters(Object[] parameters) throws IOException { 257 | if (parameters == null) return; 258 | for (int i = 0; i < parameters.length - 1; i+=2) { 259 | setParameter(parameters[i].toString(), parameters[i+1]); 260 | } 261 | } 262 | 263 | /** 264 | * posts the requests to the server, with all the cookies and parameters that were added 265 | * @return input stream with the server response 266 | * @throws IOException 267 | */ 268 | public InputStream post() throws IOException { 269 | boundary(); 270 | writeln("--"); 271 | os.close(); 272 | return connection.getInputStream(); 273 | } 274 | 275 | /** 276 | * posts the requests to the server, with all the cookies and parameters that were added before (if any), and with parameters that are passed in the argument 277 | * @param parameters request parameters 278 | * @return input stream with the server response 279 | * @throws IOException 280 | * @see setParameters 281 | */ 282 | @SuppressWarnings("unchecked") 283 | public InputStream post(Map parameters) throws IOException { 284 | setParameters(parameters); 285 | return post(); 286 | } 287 | 288 | /** 289 | * posts the requests to the server, with all the cookies and parameters that were added before (if any), and with parameters that are passed in the argument 290 | * @param parameters request parameters 291 | * @return input stream with the server response 292 | * @throws IOException 293 | * @see setParameters 294 | */ 295 | public InputStream post(Object[] parameters) throws IOException { 296 | setParameters(parameters); 297 | return post(); 298 | } 299 | 300 | /** 301 | * posts the requests to the server, with all the cookies and parameters that were added before (if any), and with cookies and parameters that are passed in the arguments 302 | * @param cookies request cookies 303 | * @param parameters request parameters 304 | * @return input stream with the server response 305 | * @throws IOException 306 | * @see setParameters 307 | * @see setCookies 308 | */ 309 | @SuppressWarnings("unchecked") 310 | public InputStream post(Map cookies, Map parameters) throws IOException { 311 | setCookies(cookies); 312 | setParameters(parameters); 313 | return post(); 314 | } 315 | 316 | /** 317 | * posts the requests to the server, with all the cookies and parameters that were added before (if any), and with cookies and parameters that are passed in the arguments 318 | * @param cookies request cookies 319 | * @param parameters request parameters 320 | * @return input stream with the server response 321 | * @throws IOException 322 | * @see setParameters 323 | * @see setCookies 324 | */ 325 | public InputStream post(String[] cookies, Object[] parameters) throws IOException { 326 | setCookies(cookies); 327 | setParameters(parameters); 328 | return post(); 329 | } 330 | 331 | /** 332 | * post the POST request to the server, with the specified parameter 333 | * @param name parameter name 334 | * @param value parameter value 335 | * @return input stream with the server response 336 | * @throws IOException 337 | * @see setParameter 338 | */ 339 | public InputStream post(String name, Object value) throws IOException { 340 | setParameter(name, value); 341 | return post(); 342 | } 343 | 344 | /** 345 | * post the POST request to the server, with the specified parameters 346 | * @param name1 first parameter name 347 | * @param value1 first parameter value 348 | * @param name2 second parameter name 349 | * @param value2 second parameter value 350 | * @return input stream with the server response 351 | * @throws IOException 352 | * @see setParameter 353 | */ 354 | public InputStream post(String name1, Object value1, String name2, Object value2) throws IOException { 355 | setParameter(name1, value1); 356 | return post(name2, value2); 357 | } 358 | 359 | /** 360 | * post the POST request to the server, with the specified parameters 361 | * @param name1 first parameter name 362 | * @param value1 first parameter value 363 | * @param name2 second parameter name 364 | * @param value2 second parameter value 365 | * @param name3 third parameter name 366 | * @param value3 third parameter value 367 | * @return input stream with the server response 368 | * @throws IOException 369 | * @see setParameter 370 | */ 371 | public InputStream post(String name1, Object value1, String name2, Object value2, String name3, Object value3) throws IOException { 372 | setParameter(name1, value1); 373 | return post(name2, value2, name3, value3); 374 | } 375 | 376 | /** 377 | * post the POST request to the server, with the specified parameters 378 | * @param name1 first parameter name 379 | * @param value1 first parameter value 380 | * @param name2 second parameter name 381 | * @param value2 second parameter value 382 | * @param name3 third parameter name 383 | * @param value3 third parameter value 384 | * @param name4 fourth parameter name 385 | * @param value4 fourth parameter value 386 | * @return input stream with the server response 387 | * @throws IOException 388 | * @see setParameter 389 | */ 390 | public InputStream post(String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4) throws IOException { 391 | setParameter(name1, value1); 392 | return post(name2, value2, name3, value3, name4, value4); 393 | } 394 | 395 | /** 396 | * posts a new request to specified URL, with parameters that are passed in the argument 397 | * @param parameters request parameters 398 | * @return input stream with the server response 399 | * @throws IOException 400 | * @see setParameters 401 | */ 402 | @SuppressWarnings("unchecked") 403 | public static InputStream post(URL url, Map parameters) throws IOException { 404 | return new ClientHttpRequest(url).post(parameters); 405 | } 406 | 407 | /** 408 | * posts a new request to specified URL, with parameters that are passed in the argument 409 | * @param parameters request parameters 410 | * @return input stream with the server response 411 | * @throws IOException 412 | * @see setParameters 413 | */ 414 | public static InputStream post(URL url, Object[] parameters) throws IOException { 415 | return new ClientHttpRequest(url).post(parameters); 416 | } 417 | 418 | /** 419 | * posts a new request to specified URL, with cookies and parameters that are passed in the argument 420 | * @param cookies request cookies 421 | * @param parameters request parameters 422 | * @return input stream with the server response 423 | * @throws IOException 424 | * @see setCookies 425 | * @see setParameters 426 | */ 427 | @SuppressWarnings("unchecked") 428 | public static InputStream post(URL url, Map cookies, Map parameters) throws IOException { 429 | return new ClientHttpRequest(url).post(cookies, parameters); 430 | } 431 | 432 | /** 433 | * posts a new request to specified URL, with cookies and parameters that are passed in the argument 434 | * @param cookies request cookies 435 | * @param parameters request parameters 436 | * @return input stream with the server response 437 | * @throws IOException 438 | * @see setCookies 439 | * @see setParameters 440 | */ 441 | public static InputStream post(URL url, String[] cookies, Object[] parameters) throws IOException { 442 | return new ClientHttpRequest(url).post(cookies, parameters); 443 | } 444 | 445 | /** 446 | * post the POST request specified URL, with the specified parameter 447 | * @param name parameter name 448 | * @param value parameter value 449 | * @return input stream with the server response 450 | * @throws IOException 451 | * @see setParameter 452 | */ 453 | public static InputStream post(URL url, String name1, Object value1) throws IOException { 454 | return new ClientHttpRequest(url).post(name1, value1); 455 | } 456 | 457 | /** 458 | * post the POST request to specified URL, with the specified parameters 459 | * @param name1 first parameter name 460 | * @param value1 first parameter value 461 | * @param name2 second parameter name 462 | * @param value2 second parameter value 463 | * @return input stream with the server response 464 | * @throws IOException 465 | * @see setParameter 466 | */ 467 | public static InputStream post(URL url, String name1, Object value1, String name2, Object value2) throws IOException { 468 | return new ClientHttpRequest(url).post(name1, value1, name2, value2); 469 | } 470 | 471 | /** 472 | * post the POST request to specified URL, with the specified parameters 473 | * @param name1 first parameter name 474 | * @param value1 first parameter value 475 | * @param name2 second parameter name 476 | * @param value2 second parameter value 477 | * @param name3 third parameter name 478 | * @param value3 third parameter value 479 | * @return input stream with the server response 480 | * @throws IOException 481 | * @see setParameter 482 | */ 483 | public static InputStream post(URL url, String name1, Object value1, String name2, Object value2, String name3, Object value3) throws IOException { 484 | return new ClientHttpRequest(url).post(name1, value1, name2, value2, name3, value3); 485 | } 486 | 487 | /** 488 | * post the POST request to specified URL, with the specified parameters 489 | * @param name1 first parameter name 490 | * @param value1 first parameter value 491 | * @param name2 second parameter name 492 | * @param value2 second parameter value 493 | * @param name3 third parameter name 494 | * @param value3 third parameter value 495 | * @param name4 fourth parameter name 496 | * @param value4 fourth parameter value 497 | * @return input stream with the server response 498 | * @throws IOException 499 | * @see setParameter 500 | */ 501 | public static InputStream post(URL url, String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4) throws IOException { 502 | return new ClientHttpRequest(url).post(name1, value1, name2, value2, name3, value3, name4, value4); 503 | } 504 | } 505 | 506 | --------------------------------------------------------------------------------