/src/mpv5/utils/http/HttpClient.java
Java | 115 lines | 58 code | 10 blank | 47 comment | 1 complexity | 5b7a315ad359936cc8d742f3273c9cc7 MD5 | raw file
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 */ 17package mpv5.utils.http; 18 19import java.io.IOException; 20import java.net.Socket; 21import java.net.URL; 22import java.net.UnknownHostException; 23import java.util.logging.Level; 24import java.util.logging.Logger; 25import mpv5.logging.Log; 26import org.apache.http.*; 27import org.apache.http.impl.*; 28import org.apache.http.message.BasicHttpRequest; 29import org.apache.http.params.*; 30import org.apache.http.protocol.*; 31import org.apache.http.util.EntityUtils; 32 33/** 34 * Use this class to make http requests 35 */ 36public class HttpClient { 37 38 private DefaultHttpClientConnection conn; 39 private final DefaultConnectionReuseStrategy connStrategy; 40 private final HttpRequestExecutor httpexecutor; 41 private final BasicHttpContext context; 42 private final HttpHost host; 43 private final BasicHttpParams params; 44 private final BasicHttpProcessor httpproc; 45 46 /** 47 * Connects to the given host 48 * @param toHost 49 * @param port 50 * @throws UnknownHostException 51 * @throws IOException 52 * @throws HttpException 53 */ 54 public HttpClient(String toHost, int port) throws UnknownHostException, IOException, HttpException { 55 params = new BasicHttpParams(); 56 HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 57 HttpProtocolParams.setContentCharset(params, "UTF-8"); 58 HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1"); 59 HttpProtocolParams.setUseExpectContinue(params, true); 60 61 httpproc = new BasicHttpProcessor(); 62 // Required protocol interceptors 63 httpproc.addInterceptor(new RequestContent()); 64 httpproc.addInterceptor(new RequestTargetHost()); 65 // Recommended protocol interceptors 66 httpproc.addInterceptor(new RequestConnControl()); 67 httpproc.addInterceptor(new RequestUserAgent()); 68 httpproc.addInterceptor(new RequestExpectContinue()); 69 httpexecutor = new HttpRequestExecutor(); 70 context = new BasicHttpContext(null); 71 host = new HttpHost(toHost, port); 72 conn = new DefaultHttpClientConnection(); 73 connStrategy = new DefaultConnectionReuseStrategy(); 74 context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); 75 context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host); 76 } 77 78 /** 79 * Runs a GET request 80 * @param srequest The request, eg /servlets-examples/servlet/RequestInfoExample" 81 * @return 82 * @throws UnknownHostException 83 * @throws IOException 84 * @throws HttpException 85 */ 86 public HttpResponse request(String srequest) throws UnknownHostException, IOException, HttpException { 87 88 if (!conn.isOpen()) { 89 Socket socket = new Socket(host.getHostName(), host.getPort()); 90 conn.bind(socket, params); 91 92 BasicHttpRequest request = new BasicHttpRequest("GET", srequest); 93 Log.Debug(this, ">> Request URI: " + request.getRequestLine().getUri()); 94 95 request.setParams(params); 96 httpexecutor.preProcess(request, httpproc, context); 97 HttpResponse response = httpexecutor.execute(request, conn, context); 98 response.setParams(params); 99 httpexecutor.postProcess(response, httpproc, context); 100 101// Log.Debug(this, "<< Response: " + response.getStatusLine()); 102// Log.Debug(this, EntityUtils.toString(response.getEntity())); 103// Log.Debug(this, "=============="); 104// if (!connStrategy.keepAlive(response, context)) { 105// conn.close(); 106// } else { 107// Log.Debug(this, "Connection kept alive..."); 108// } 109// } else { 110// Log.Debug(this, "Connection already closed."); 111 return response; 112 } 113 return null; 114 } 115}