PageRenderTime 24ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/utils/http/HttpClient.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 115 lines | 58 code | 10 blank | 47 comment | 1 complexity | 5b7a315ad359936cc8d742f3273c9cc7 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  1. /*
  2. * This file is part of YaBS.
  3. *
  4. * YaBS is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * YaBS is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with YaBS. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package mpv5.utils.http;
  18. import java.io.IOException;
  19. import java.net.Socket;
  20. import java.net.URL;
  21. import java.net.UnknownHostException;
  22. import java.util.logging.Level;
  23. import java.util.logging.Logger;
  24. import mpv5.logging.Log;
  25. import org.apache.http.*;
  26. import org.apache.http.impl.*;
  27. import org.apache.http.message.BasicHttpRequest;
  28. import org.apache.http.params.*;
  29. import org.apache.http.protocol.*;
  30. import org.apache.http.util.EntityUtils;
  31. /**
  32. * Use this class to make http requests
  33. */
  34. public class HttpClient {
  35. private DefaultHttpClientConnection conn;
  36. private final DefaultConnectionReuseStrategy connStrategy;
  37. private final HttpRequestExecutor httpexecutor;
  38. private final BasicHttpContext context;
  39. private final HttpHost host;
  40. private final BasicHttpParams params;
  41. private final BasicHttpProcessor httpproc;
  42. /**
  43. * Connects to the given host
  44. * @param toHost
  45. * @param port
  46. * @throws UnknownHostException
  47. * @throws IOException
  48. * @throws HttpException
  49. */
  50. public HttpClient(String toHost, int port) throws UnknownHostException, IOException, HttpException {
  51. params = new BasicHttpParams();
  52. HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
  53. HttpProtocolParams.setContentCharset(params, "UTF-8");
  54. HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
  55. HttpProtocolParams.setUseExpectContinue(params, true);
  56. httpproc = new BasicHttpProcessor();
  57. // Required protocol interceptors
  58. httpproc.addInterceptor(new RequestContent());
  59. httpproc.addInterceptor(new RequestTargetHost());
  60. // Recommended protocol interceptors
  61. httpproc.addInterceptor(new RequestConnControl());
  62. httpproc.addInterceptor(new RequestUserAgent());
  63. httpproc.addInterceptor(new RequestExpectContinue());
  64. httpexecutor = new HttpRequestExecutor();
  65. context = new BasicHttpContext(null);
  66. host = new HttpHost(toHost, port);
  67. conn = new DefaultHttpClientConnection();
  68. connStrategy = new DefaultConnectionReuseStrategy();
  69. context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
  70. context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);
  71. }
  72. /**
  73. * Runs a GET request
  74. * @param srequest The request, eg /servlets-examples/servlet/RequestInfoExample"
  75. * @return
  76. * @throws UnknownHostException
  77. * @throws IOException
  78. * @throws HttpException
  79. */
  80. public HttpResponse request(String srequest) throws UnknownHostException, IOException, HttpException {
  81. if (!conn.isOpen()) {
  82. Socket socket = new Socket(host.getHostName(), host.getPort());
  83. conn.bind(socket, params);
  84. BasicHttpRequest request = new BasicHttpRequest("GET", srequest);
  85. Log.Debug(this, ">> Request URI: " + request.getRequestLine().getUri());
  86. request.setParams(params);
  87. httpexecutor.preProcess(request, httpproc, context);
  88. HttpResponse response = httpexecutor.execute(request, conn, context);
  89. response.setParams(params);
  90. httpexecutor.postProcess(response, httpproc, context);
  91. // Log.Debug(this, "<< Response: " + response.getStatusLine());
  92. // Log.Debug(this, EntityUtils.toString(response.getEntity()));
  93. // Log.Debug(this, "==============");
  94. // if (!connStrategy.keepAlive(response, context)) {
  95. // conn.close();
  96. // } else {
  97. // Log.Debug(this, "Connection kept alive...");
  98. // }
  99. // } else {
  100. // Log.Debug(this, "Connection already closed.");
  101. return response;
  102. }
  103. return null;
  104. }
  105. }