hal/src/zutil/net/http/HttpClient.java

152 lines
4.3 KiB
Java
Raw Normal View History

2015-05-27 13:13:19 +00:00
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Ziver Koc
2013-05-28 19:29:24 +00:00
*
2011-07-13 17:53:17 +00:00
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
2013-05-28 19:29:24 +00:00
*
2011-07-13 17:53:17 +00:00
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
2013-05-28 19:29:24 +00:00
*
2011-07-13 17:53:17 +00:00
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
2015-05-27 13:13:19 +00:00
*/
2013-05-28 19:29:24 +00:00
2011-06-24 23:20:59 +00:00
package zutil.net.http;
import zutil.net.http.HttpPrintStream.HttpMessageType;
2011-06-24 23:20:59 +00:00
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.URL;
import java.util.HashMap;
/**
* This class connects to a HTTP server and
* parses the result
*
* @author Ziver
*/
2015-12-19 13:32:34 +01:00
public class HttpClient implements AutoCloseable{
public static enum HttpRequestType{
2011-06-24 23:20:59 +00:00
GET, POST
}
2015-12-19 13:32:34 +01:00
// Request variables
2011-06-24 23:20:59 +00:00
private HttpURL url;
private HttpRequestType type;
private HashMap<String,String> headers;
private HashMap<String,String> cookies;
2015-12-19 13:32:34 +01:00
private String data;
// Response variables
private HttpHeaderParser rspHeader;
private BufferedReader rspReader;
2011-06-24 23:20:59 +00:00
2015-12-19 13:32:34 +01:00
public HttpClient(HttpRequestType type){
2011-06-24 23:20:59 +00:00
this.type = type;
headers = new HashMap<String,String>();
cookies = new HashMap<String,String>();
}
public void setURL( URL url){
this.url = new HttpURL( url );
}
/**
* Adds a parameter to the request
*/
public void setParameter( String key, String value ){
url.setParameter(key, value);
}
/**
* Adds a cookie to the request
*/
public void setCookie( String key, String value ){
cookies.put(key, value);
}
/**
* Adds a header value to the request
*/
public void setHeader( String key, String value ){
headers.put(key, value);
}
2015-12-19 13:32:34 +01:00
/**
* Sets the content data that will be included in the request.
* NOTE: this will override the POST data parameter inclusion.
*/
public void setData( String data ){
this.data = data;
}
/**
* Will send a HTTP request to the target host.
* NOTE: any previous request connections will be closed
*/
2011-06-24 23:20:59 +00:00
public HttpHeaderParser send() throws IOException{
Socket conn = new Socket( url.getHost(), url.getPort());
// Request
HttpPrintStream request = new HttpPrintStream( conn.getOutputStream(), HttpMessageType.REQUEST );
request.setRequestType( type.toString() );
request.setRequestURL( url.getHttpURL() );
request.setHeaders( headers );
request.setCookies( cookies );
if( type == HttpRequestType.POST ){
2015-12-19 13:32:34 +01:00
String postData = null;
if(data != null)
postData = data;
else
postData = url.getParameterString();
request.setHeader("Content-Length", ""+postData.length());
2011-06-24 23:20:59 +00:00
request.println();
2015-12-19 13:32:34 +01:00
request.print( postData );
2011-06-24 23:20:59 +00:00
}
else
request.println();
request.close();
2011-06-24 23:20:59 +00:00
// Response
2015-12-19 13:32:34 +01:00
if(rspHeader != null || rspReader != null) // Close previous request
this.close();
rspReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
rspHeader = new HttpHeaderParser( rspReader );
return rspHeader;
2011-06-24 23:20:59 +00:00
}
2015-12-19 13:32:34 +01:00
public HttpHeaderParser getResponseHeader(){
return rspHeader;
}
public BufferedReader getResponseReader(){
return rspReader;
}
@Override
public void close() throws IOException {
if(rspReader != null)
rspReader.close();
rspReader = null;
rspHeader = null;
}
2011-06-24 23:20:59 +00:00
}