Hi there,
I hope the following code might help you. I have retrieve data from a URL with
this code.
Gargi
/*************************************************/
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.*;
public class HttpPageLoader extends HttpServlet
{
public void init(ServletConfig config) throws ServletException
{
super.init(config);
System.getProperties().put( "proxySet", "true" );
System.getProperties().put( "proxyHost", "314.7.66.8" );
System.getProperties().put( "proxyPort", Integer.toString(3128) );
}
public void service (HttpServletRequest iReq, HttpServletResponse iRes)
throws ServletException, IOException
{
InputStream in;
String xxx = iReq.getParameter("UrlString");
String errorStr = "";
String inStr = "";
StringBuffer iData = new StringBuffer("");
// iRes.setContentType("text/html");
ServletOutputStream out = iRes.getOutputStream();
try
{
URL url = new URL( xxx );
HttpURLConnection conn;
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput (true);
conn.setDoOutput(true);
if( conn.getResponseCode() != HttpURLConnection.HTTP_OK )
{ int iResponseCode = conn.getResponseCode();
//throw new Exception("ERROR handling URLConnection " +
conn.getResponseCode());
}
String ctype = conn.getContentType().toLowerCase();
if(! ctype.equals("text/html") )
{ //throw new Exception("ERROR handling URLConnection, content type is not html:
" + conn.getContentType());
}
in = conn.getInputStream();
BufferedReader reader = new BufferedReader( new InputStreamReader(in) );
while( (inStr = reader.readLine()) != null )
{ iData.append(inStr + "\n");
if (iData.length() > 1000) break;
//out.println(inStr);
}
reader.close();
}
catch(UnknownHostException uhe)
{
errorStr = "Page.loadFromWWW() Unknown host " + uhe;
// throw new UnknownHostException(uhe.getMessage());
}
catch(IOException e)
{
errorStr = "Page.loadFromWWW() = " + e;
// throw new IOException(e.getMessage());
}
// out.println("<html>");
// out.println("<head><title>Hello World</title></head>");
// out.println("<body>");
out.println(xxx);
out.println("<br>");
out.println(iData.toString());
out.println("<br>");
out.println(errorStr);
out.println("<br>");
// out.println("</body></html>");
}
}
/*********************************************************/
Pinghua Young wrote:
> Has anyone successfully opened a HTTPS url connection to a web site,
> and retrieved its content from within a servlet?
>
> I am trying to use Sun's early access JSSE package, but I am having
> trouble getting it to work.
>
> Here's the exception I got:
>
> java.lang.Exception: Stack trace
> at java.lang.Thread.dumpStack(Thread.java:983)
> at com.sun.net.ssl.internal.ssl.SSLSocketImpl.a([DashoPro-V1.2-120198])
> at com.sun.net.ssl.internal.ssl.ClientHandshaker.a([DashoPro-V1.2-120198
> ])
> at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage([DashoPr
> o-V1.2-120198])
> at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Compiled Code)
>
> at com.sun.net.ssl.internal.ssl.SSLSocketImpl.a(Compiled Code)
> at com.sun.net.ssl.internal.ssl.SSLSocketImpl.a(Compiled Code)
> at com.sun.net.ssl.internal.ssl.AppOutputStream.write(Compiled Code)
> at java.io.OutputStream.write(OutputStream.java:65)
> at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake([DashoPro-V
> 1.2-120198])
> at com.sun.net.ssl.internal.www.https.HttpsClient.doConnect([DashoPro-V1
> .2-120198])
> at sun.net.www.http.HttpClient.openServer(HttpClient.java:320)
> at sun.net.www.http.HttpClient.openServer(HttpClient.java:388)
> at sun.net.www.http.HttpClient.<init>(HttpClient.java:260)
> at sun.net.www.http.HttpClient.<init>(HttpClient.java:265)
> at com.sun.net.ssl.internal.www.https.HttpsClient.<init>([DashoPro-V1.2-
> 120198])
> at com.sun.net.ssl.internal.www.https.HttpsClient.New([DashoPro-V1.2-120
> 198])
> at com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnection.connec
> t([DashoPro-V1.2-120198])
> at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Compiled C
> ode)
> at HttpsTest.main(Compiled Code)
> javax.net.ssl.SSLException: untrusted server cert chain
> at com.sun.net.ssl.internal.ssl.SSLSocketImpl.a([DashoPro-V1.2-120198])
> at com.sun.net.ssl.internal.ssl.ClientHandshaker.a([DashoPro-V1.2-120198
> ])
> at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage([DashoPr
> o-V1.2-120198])
> at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Compiled Code)
>
> at com.sun.net.ssl.internal.ssl.SSLSocketImpl.a(Compiled Code)
> at com.sun.net.ssl.internal.ssl.SSLSocketImpl.a(Compiled Code)
> at com.sun.net.ssl.internal.ssl.AppOutputStream.write(Compiled Code)
> at java.io.OutputStream.write(OutputStream.java:65)
> at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake([DashoPro-V
> 1.2-120198])
> at com.sun.net.ssl.internal.www.https.HttpsClient.doConnect([DashoPro-V1
> .2-120198])
> at sun.net.www.http.HttpClient.openServer(HttpClient.java:320)
> at sun.net.www.http.HttpClient.openServer(HttpClient.java:388)
> at sun.net.www.http.HttpClient.<init>(HttpClient.java:260)
> at sun.net.www.http.HttpClient.<init>(HttpClient.java:265)
> at com.sun.net.ssl.internal.www.https.HttpsClient.<init>([DashoPro-V1.2-
> 120198])
> at com.sun.net.ssl.internal.www.https.HttpsClient.New([DashoPro-V1.2-120
> 198])
> at com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnection.connec
> t([DashoPro-V1.2-120198])
> at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Compiled C
> ode)
> at HttpsTest.main(Compiled Code)
>
> ___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html