OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 20 matching lines...) Expand all Loading... |
31 private String contentType; | 31 private String contentType; |
32 | 32 |
33 /** | 33 /** |
34 * Http requests callbacks. | 34 * Http requests callbacks. |
35 */ | 35 */ |
36 public interface AsyncHttpEvents { | 36 public interface AsyncHttpEvents { |
37 void onHttpError(String errorMessage); | 37 void onHttpError(String errorMessage); |
38 void onHttpComplete(String response); | 38 void onHttpComplete(String response); |
39 } | 39 } |
40 | 40 |
41 public AsyncHttpURLConnection(String method, String url, String message, | 41 public AsyncHttpURLConnection(String method, String url, String message, Async
HttpEvents events) { |
42 AsyncHttpEvents events) { | |
43 this.method = method; | 42 this.method = method; |
44 this.url = url; | 43 this.url = url; |
45 this.message = message; | 44 this.message = message; |
46 this.events = events; | 45 this.events = events; |
47 } | 46 } |
48 | 47 |
49 public void setContentType(String contentType) { | 48 public void setContentType(String contentType) { |
50 this.contentType = contentType; | 49 this.contentType = contentType; |
51 } | 50 } |
52 | 51 |
53 public void send() { | 52 public void send() { |
54 Runnable runHttp = new Runnable() { | 53 Runnable runHttp = new Runnable() { |
55 public void run() { | 54 public void run() { |
56 sendHttpMessage(); | 55 sendHttpMessage(); |
57 } | 56 } |
58 }; | 57 }; |
59 new Thread(runHttp).start(); | 58 new Thread(runHttp).start(); |
60 } | 59 } |
61 | 60 |
62 private void sendHttpMessage() { | 61 private void sendHttpMessage() { |
63 try { | 62 try { |
64 HttpURLConnection connection = | 63 HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnec
tion(); |
65 (HttpURLConnection) new URL(url).openConnection(); | |
66 byte[] postData = new byte[0]; | 64 byte[] postData = new byte[0]; |
67 if (message != null) { | 65 if (message != null) { |
68 postData = message.getBytes("UTF-8"); | 66 postData = message.getBytes("UTF-8"); |
69 } | 67 } |
70 connection.setRequestMethod(method); | 68 connection.setRequestMethod(method); |
71 connection.setUseCaches(false); | 69 connection.setUseCaches(false); |
72 connection.setDoInput(true); | 70 connection.setDoInput(true); |
73 connection.setConnectTimeout(HTTP_TIMEOUT_MS); | 71 connection.setConnectTimeout(HTTP_TIMEOUT_MS); |
74 connection.setReadTimeout(HTTP_TIMEOUT_MS); | 72 connection.setReadTimeout(HTTP_TIMEOUT_MS); |
75 // TODO(glaznev) - query request origin from pref_room_server_url_key pref
erences. | 73 // TODO(glaznev) - query request origin from pref_room_server_url_key pref
erences. |
(...skipping 13 matching lines...) Expand all Loading... |
89 // Send POST request. | 87 // Send POST request. |
90 if (doOutput && postData.length > 0) { | 88 if (doOutput && postData.length > 0) { |
91 OutputStream outStream = connection.getOutputStream(); | 89 OutputStream outStream = connection.getOutputStream(); |
92 outStream.write(postData); | 90 outStream.write(postData); |
93 outStream.close(); | 91 outStream.close(); |
94 } | 92 } |
95 | 93 |
96 // Get response. | 94 // Get response. |
97 int responseCode = connection.getResponseCode(); | 95 int responseCode = connection.getResponseCode(); |
98 if (responseCode != 200) { | 96 if (responseCode != 200) { |
99 events.onHttpError("Non-200 response to " + method + " to URL: " | 97 events.onHttpError("Non-200 response to " + method + " to URL: " + url +
" : " |
100 + url + " : " + connection.getHeaderField(null)); | 98 + connection.getHeaderField(null)); |
101 connection.disconnect(); | 99 connection.disconnect(); |
102 return; | 100 return; |
103 } | 101 } |
104 InputStream responseStream = connection.getInputStream(); | 102 InputStream responseStream = connection.getInputStream(); |
105 String response = drainStream(responseStream); | 103 String response = drainStream(responseStream); |
106 responseStream.close(); | 104 responseStream.close(); |
107 connection.disconnect(); | 105 connection.disconnect(); |
108 events.onHttpComplete(response); | 106 events.onHttpComplete(response); |
109 } catch (SocketTimeoutException e) { | 107 } catch (SocketTimeoutException e) { |
110 events.onHttpError("HTTP " + method + " to " + url + " timeout"); | 108 events.onHttpError("HTTP " + method + " to " + url + " timeout"); |
111 } catch (IOException e) { | 109 } catch (IOException e) { |
112 events.onHttpError("HTTP " + method + " to " + url + " error: " | 110 events.onHttpError("HTTP " + method + " to " + url + " error: " + e.getMes
sage()); |
113 + e.getMessage()); | |
114 } | 111 } |
115 } | 112 } |
116 | 113 |
117 // Return the contents of an InputStream as a String. | 114 // Return the contents of an InputStream as a String. |
118 private static String drainStream(InputStream in) { | 115 private static String drainStream(InputStream in) { |
119 Scanner s = new Scanner(in).useDelimiter("\\A"); | 116 Scanner s = new Scanner(in).useDelimiter("\\A"); |
120 return s.hasNext() ? s.next() : ""; | 117 return s.hasNext() ? s.next() : ""; |
121 } | 118 } |
122 } | 119 } |
OLD | NEW |