Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* Copyright 2006 Sun Microsystems, Inc.  All rights reserved.  You may not modify, use, reproduce, or distribute this software except in compliance with the terms of the License at: 
   http://developer.sun.com/berkeley_license.html
   $Id: HttpClient.java,v 1.1 2006/05/17 07:34:35 gmurray71 Exp $ 
  */
  
  package com.sun.jmaki.rss;
  
  import java.io.*;
  import java.net.*;

Author(s):
Yutaka Yoshida Minimum set of HTTPclient supporting both http and https. It's aslo capable of POST, but it doesn't provide doGet because the caller can just read the inputstream.
 
 public class HttpClient {
     private String proxyHost = null;
     private String proxyPort = null;
     private boolean isHttps = false;
     private boolean isProxy = false;
     private URLConnection urlConnection = null;
    
    

Parameters:
url URL string
 
     public HttpClient(String url) {
         this. = getURLConnection(url);
     }
    

Parameters:
phost PROXY host name
pport PROXY port string
url URL string
 
     public HttpClient(String phostString pportString url) {
         if (phost != null && pport != null) {
             this. = true;
         }
         this. = phost;
         this. = pport;
         if (url.indexOf("https") >= 0) {
              = true;
         }
         this. = getURLConnection(url);
     }
    
    
private method to get the URLConnection

Parameters:
str URL string
 
     private URLConnection getURLConnection(String str) {
         try {
             if () {
                 /* when communicating with the server which has unsigned or invalid
                  * certificate (https), SSLException or IOException is thrown.
                  * the following line is a hack to avoid that
                  */
                 Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
                 System.setProperty("java.protocol.handler.pkgs""com.sun.net.ssl.internal.www.protocol");
                 if () {
                     System.setProperty("https.proxyHost");
                     System.setProperty("https.proxyPort");
                 }
             } else {
                 if () {
                     System.setProperty("http.proxyHost");
                     System.setProperty("http.proxyPort");
                 }
             }
             URL url = new URL(str);
             return (url.openConnection());
             
         } catch (MalformedURLException me) {
             ..println("Malformed URL");
             me.printStackTrace();
             return null;
         } catch (Exception e) {
             e.printStackTrace();
             return null;
         }
     }
    
    
returns the inputstream from URLConnection

Returns:
InputStream
 
     public InputStream getInputStream() {
         try {
             return (this..getInputStream());
         } catch (Exception e) {
             e.printStackTrace();
             return null;
         }
     }
    
    
return the OutputStream from URLConnection

Returns:
OutputStream
    public OutputStream getOutputStream() {
        
        try {
            return (this..getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    
posts data to the inputstream and returns the InputStream.

Parameters:
postData data to be posted. must be url-encoded already.
Returns:
InputStream input stream from URLConnection
    public InputStream doPost(String postData) {
        this..setDoOutput(true);
        OutputStream os = this.getOutputStream();
        PrintStream ps = new PrintStream(os);
        ps.print(postData);
        ps.close();
        
        return (this.getInputStream());
    }
    
    public String getContentEncoding() {
        return (this..getContentEncoding());
    }
    public int getContentLength() {
        return (this..getContentLength());
    }
    public String getContentType() {
        return (this..getContentType());
    }
    public long getDate() {
        return (this..getDate());
    }
    public String getHeader(String name) {
        return (this..getHeaderField(name));
    }
    public long getIfModifiedSince() {
        return (this..getIfModifiedSince());
    }
New to GrepCode? Check out our FAQ X