Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class HTTPRequest {
- public enum HTTPStates {
- CONNECTED,
- CONNECTION_FAILED,
- GETTING,
- TIMEOUT,
- CANCELLED,
- OK,
- NO_CONTENT,
- REDIRECTION,
- BAD_REQUEST,
- FORBIDDEN,
- NOT_FOUND,
- SERVER_ERROR,
- BAD_GATEWAY,
- SERVICE_UNAVAILABLE,
- UNKNOWN
- }
- Object m_lock = new Object();
- URL m_url;
- Thread m_thread;
- String m_urlString;
- boolean m_complete;
- boolean m_cancelled;
- boolean m_success;
- String m_result;
- HTTPStates m_state;
- public HTTPRequest(String _url) throws HTTPRequestException {
- m_urlString = _url;
- try {
- m_url = new URL(_url);
- if (!m_url.getProtocol().toLowerCase().equals("http"))
- throw new HTTPRequestException("Not an HTTP URL");
- } catch (MalformedURLException e) {
- throw new HTTPRequestException("Invalid URL");
- }
- m_cancelled = false;
- m_complete = false;
- m_success = false;
- m_result = null;
- m_thread = new Thread(new HTTPRequestThread(this));
- m_thread.start();
- }
- String getURL() {
- return this.m_urlString;
- }
- void cancel() {
- synchronized (m_lock) {
- m_cancelled = true;
- }
- }
- public boolean isComplete() {
- synchronized (m_lock) {
- return m_complete;
- }
- }
- public boolean wasSuccessful() {
- synchronized (m_lock) {
- return m_success;
- }
- }
- public HTTPStates getState() {
- synchronized (m_lock) {
- return m_state;
- }
- }
- public BufferedReader getContents() {
- String result = null;
- synchronized (result) {
- result = m_result;
- }
- if (result != null)
- return new BufferedReader(new StringReader(result));
- return null;
- }
- private class HTTPRequestThread implements Runnable {
- private HTTPRequest request;
- private HttpURLConnection conn;
- public HTTPRequestThread(HTTPRequest request) {
- this.request = request;
- }
- public void run() {
- try {
- conn = (HttpURLConnection) request.m_url.openConnection();
- conn.setReadTimeout(10000);
- try {
- conn.connect();
- request.m_state = HTTPStates.CONNECTED;
- } catch (IOException ex) {
- synchronized (request.m_lock) {
- request.m_complete = true;
- request.m_success = false;
- request.m_result = null;
- request.m_state = HTTPStates.CONNECTION_FAILED;
- }
- }
- BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
- StringBuilder result = new StringBuilder();
- while (true) {
- synchronized (request.m_lock) {
- if (request.m_cancelled) {
- request.m_state = HTTPStates.CANCELLED;
- break;
- }
- }
- String line = reader.readLine();
- if (line == null)
- break;
- result.append(line);
- result.append('\n');
- request.m_state = HTTPStates.GETTING;
- }
- reader.close();
- synchronized (request.m_lock) {
- if (request.m_cancelled) {
- request.m_complete = true;
- request.m_success = false;
- request.m_result = null;
- request.m_state = HTTPStates.CANCELLED;
- } else {
- request.m_complete = true;
- request.m_success = true;
- request.m_result = result.toString();
- setState(conn.getResponseCode());
- }
- }
- } catch (ConnectException e) {
- synchronized (request.m_lock) {
- request.m_complete = true;
- request.m_success = false;
- request.m_result = null;
- request.m_state = HTTPStates.TIMEOUT;
- }
- } catch (IOException e) {
- synchronized (request.m_lock) {
- request.m_complete = true;
- request.m_success = false;
- request.m_result = null;
- setState(0);
- }
- } finally {
- conn.disconnect();
- conn = null;
- }
- }
- private void setState(int responseCode) {
- switch (responseCode) {
- case HttpURLConnection.HTTP_OK:
- request.m_state = HTTPStates.OK;
- break;
- case HttpURLConnection.HTTP_NO_CONTENT:
- request.m_state = HTTPStates.NO_CONTENT;
- break;
- case HttpURLConnection.HTTP_MOVED_PERM:
- case HttpURLConnection.HTTP_MOVED_TEMP:
- request.m_state = HTTPStates.REDIRECTION;
- break;
- case HttpURLConnection.HTTP_BAD_REQUEST:
- request.m_state = HTTPStates.BAD_REQUEST;
- break;
- case HttpURLConnection.HTTP_FORBIDDEN:
- request.m_state = HTTPStates.FORBIDDEN;
- break;
- case HttpURLConnection.HTTP_NOT_FOUND:
- request.m_state = HTTPStates.NOT_FOUND;
- break;
- case HttpURLConnection.HTTP_INTERNAL_ERROR:
- case HttpURLConnection.HTTP_NOT_IMPLEMENTED:
- case HttpURLConnection.HTTP_GATEWAY_TIMEOUT:
- case HttpURLConnection.HTTP_VERSION:
- request.m_state = HTTPStates.SERVER_ERROR;
- break;
- case HttpURLConnection.HTTP_BAD_GATEWAY:
- request.m_state = HTTPStates.BAD_GATEWAY;
- break;
- case HttpURLConnection.HTTP_UNAVAILABLE:
- request.m_state = HTTPStates.SERVICE_UNAVAILABLE;
- break;
- default:
- request.m_state = HTTPStates.UNKNOWN;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement