Added ssl support to HttpClient and included Host header for virtual servers
This commit is contained in:
parent
40d1279890
commit
bbcb62b913
4 changed files with 38 additions and 3 deletions
|
|
@ -27,6 +27,8 @@ package zutil.net.http;
|
|||
import zutil.converter.Converter;
|
||||
import zutil.net.http.HttpPrintStream.HttpMessageType;
|
||||
|
||||
import javax.net.ssl.SSLSocket;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
|
@ -81,6 +83,13 @@ public class HttpClient implements AutoCloseable {
|
|||
this.protocol = protocol;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the type of request. Default is GET.
|
||||
*/
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public void setURL(URL url) {
|
||||
setURL(new HttpURL(url));
|
||||
}
|
||||
|
|
@ -130,7 +139,23 @@ public class HttpClient implements AutoCloseable {
|
|||
* NOTE: any previous request connections will be closed
|
||||
*/
|
||||
public HttpHeader send() throws IOException {
|
||||
Socket conn = new Socket(url.getHost(), url.getPort());
|
||||
int port = 80;
|
||||
if (url.getPort() > 0)
|
||||
port = url.getPort();
|
||||
else if ("https".equals(url.getProtocol()))
|
||||
port = 443;
|
||||
|
||||
// ---------------------------------
|
||||
// Create Socket
|
||||
// ---------------------------------
|
||||
|
||||
Socket conn;
|
||||
if ("https".equals(url.getProtocol())) {
|
||||
conn = SSLSocketFactory.getDefault().createSocket(url.getHost(), port);
|
||||
((SSLSocket )conn).startHandshake();
|
||||
} else {
|
||||
conn = new Socket(url.getHost(), port);
|
||||
}
|
||||
|
||||
// ---------------------------------
|
||||
// Request
|
||||
|
|
@ -143,6 +168,12 @@ public class HttpClient implements AutoCloseable {
|
|||
request.setHeaders(headers);
|
||||
request.setCookies(cookies);
|
||||
|
||||
// Set headers
|
||||
|
||||
request.setHeader(HttpHeader.HEADER_HOST, url.getHost() + ":" + port);
|
||||
|
||||
// send payload
|
||||
|
||||
if (HttpRequestType.POST.toString().equals(type)) {
|
||||
String postData;
|
||||
if (content != null)
|
||||
|
|
|
|||
|
|
@ -39,6 +39,7 @@ public class HttpHeader {
|
|||
public static final String HEADER_COOKIE = "Cookie";
|
||||
public static final String HEADER_CONTENT_TYPE = "Content-Type";
|
||||
public static final String HEADER_CONTENT_LENGTH = "Content-Length";
|
||||
public static final String HEADER_HOST = "Host";
|
||||
public static final String HEADER_IF_NONE_MATCH = "If-None-Match";
|
||||
public static final String HEADER_LOCATION = "Location";
|
||||
public static final String HEADER_SET_COOKIE = "Set-Cookie";
|
||||
|
|
|
|||
|
|
@ -275,6 +275,11 @@ public class HttpPrintStream extends OutputStream {
|
|||
|
||||
out.println();
|
||||
header = null;
|
||||
|
||||
// Check for errors
|
||||
|
||||
if (out.checkError())
|
||||
throw new RuntimeException("Underlying stream has thrown a error.");
|
||||
}
|
||||
|
||||
out.print(s);
|
||||
|
|
|
|||
|
|
@ -26,8 +26,6 @@ package zutil.net.http;
|
|||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue