diff options
Diffstat (limited to 'pki/base/util/src/com/netscape/cmsutil/http/HttpClient.java')
-rw-r--r-- | pki/base/util/src/com/netscape/cmsutil/http/HttpClient.java | 61 |
1 files changed, 33 insertions, 28 deletions
diff --git a/pki/base/util/src/com/netscape/cmsutil/http/HttpClient.java b/pki/base/util/src/com/netscape/cmsutil/http/HttpClient.java index 2c0134687..130d747d6 100644 --- a/pki/base/util/src/com/netscape/cmsutil/http/HttpClient.java +++ b/pki/base/util/src/com/netscape/cmsutil/http/HttpClient.java @@ -30,8 +30,9 @@ import org.mozilla.jss.ssl.SSLCertificateApprovalCallback; import com.netscape.cmsutil.net.ISocketFactory; /** - * basic http client. not optimized for performance. handles only string - * content. + * basic http client. + * not optimized for performance. + * handles only string content. */ public class HttpClient { protected ISocketFactory mFactory = null; @@ -53,19 +54,18 @@ public class HttpClient { mFactory = factory; } - public HttpClient(ISocketFactory factory, - SSLCertificateApprovalCallback certApprovalCallback) { + public HttpClient(ISocketFactory factory, SSLCertificateApprovalCallback certApprovalCallback) { mFactory = factory; mCertApprovalCallback = certApprovalCallback; } - public void connect(String host, int port) throws IOException { + public void connect(String host, int port) + throws IOException { if (mFactory != null) { if (mCertApprovalCallback == null) { mSocket = mFactory.makeSocket(host, port); } else { - mSocket = mFactory.makeSocket(host, port, - mCertApprovalCallback, null); + mSocket = mFactory.makeSocket(host, port, mCertApprovalCallback, null); } } else { mSocket = new Socket(host, port); @@ -76,7 +76,7 @@ public class HttpClient { throw e; } - + mInputStream = mSocket.getInputStream(); mOutputStream = mSocket.getOutputStream(); mInputStreamReader = new InputStreamReader(mInputStream, "UTF8"); @@ -86,7 +86,8 @@ public class HttpClient { } // Inserted by beomsuk - public void connect(String host, int port, int timeout) throws IOException { + public void connect(String host, int port, int timeout) + throws IOException { if (mFactory != null) { mSocket = mFactory.makeSocket(host, port, timeout); } else { @@ -98,7 +99,7 @@ public class HttpClient { throw e; } - + mInputStream = mSocket.getInputStream(); mOutputStream = mSocket.getOutputStream(); mInputStreamReader = new InputStreamReader(mInputStream, "UTF8"); @@ -113,11 +114,14 @@ public class HttpClient { } /** - * Sends a request to http server. Returns a http response. + * Sends a request to http server. + * Returns a http response. */ - public HttpResponse send(HttpRequest request) throws IOException { + public HttpResponse send(HttpRequest request) + throws IOException { HttpResponse resp = new HttpResponse(); + if (mOutputStream == null) throw new IOException("Output stream not initialized"); request.write(mOutputStreamWriter); @@ -132,7 +136,8 @@ public class HttpClient { return resp; } - public void disconnect() throws IOException { + public void disconnect() + throws IOException { mSocket.close(); mInputStream = null; mOutputStream = null; @@ -166,7 +171,8 @@ public class HttpClient { /** * unit test */ - public static void main(String args[]) throws Exception { + public static void main(String args[]) + throws Exception { HttpClient c = new HttpClient(); HttpRequest req = new HttpRequest(); HttpResponse resp = null; @@ -176,7 +182,7 @@ public class HttpClient { req.setMethod("GET"); req.setURI(args[2]); - if (args.length >= 4) + if (args.length >= 4) req.setHeader("Connection", args[3]); resp = c.send(req); @@ -185,30 +191,29 @@ public class HttpClient { System.out.println("reason " + resp.getReasonPhrase()); System.out.println("content " + resp.getContent()); - // String lenstr = resp.getHeader("Content-Length"); - // System.out.println("content len is "+lenstr); - // int length = Integer.parseInt(lenstr); - // char[] content = new char[length]; - // c.mBufferedReader.read(content, 0, content.length); - // System.out.println(content); + //String lenstr = resp.getHeader("Content-Length"); + //System.out.println("content len is "+lenstr); + //int length = Integer.parseInt(lenstr); + //char[] content = new char[length]; + //c.mBufferedReader.read(content, 0, content.length); + //System.out.println(content); if (args.length >= 4 && args[3].equalsIgnoreCase("keep-alive")) { int len; char[] msgbody; for (int i = 0; i < 2; i++) { - if (i == 1) - req.setHeader("Connection", "Close"); + if (i == 1) req.setHeader("Connection", "Close"); resp = c.send(req); System.out.println("version " + resp.getHttpVers()); System.out.println("status code " + resp.getStatusCode()); System.out.println("reason " + resp.getReasonPhrase()); System.out.println("content " + resp.getContent()); - // len = Integer.parseInt(resp.getHeader("Content-Length")); - // System.out.println("content len is "+len); - // msgbody = new char[len]; - // c.mBufferedReader.read(msgbody, 0, len); - // System.out.println(content); + //len = Integer.parseInt(resp.getHeader("Content-Length")); + //System.out.println("content len is "+len); + //msgbody = new char[len]; + //c.mBufferedReader.read(msgbody, 0, len); + //System.out.println(content); } } } |