Source opvragen werkt niet.

Status
Niet open voor verdere reacties.

Thijmen1985

Nieuwe gebruiker
Lid geworden
25 mei 2009
Berichten
2
Geachte lezer,

ik heb de volgende code aangepast en krijg hem niet werkend, hij zegt bij de eerste regel dat hij een semicolon verwacht maar dit is volgensmij niet het geval.

is er iemand die ziet wat er fout is?
PHP:
WebFile file = new WebFile( "http://voorbeeld.com" );
String MIME    = file.getMIMEType( );
Object content = file.getContent( );
if ( MIME.equals( "text/html" ) && content instanceof String )
{
    document.page.html.value = (String)content;
    
}

/**
 * Get a web file.
 */
public final class WebFile {
    // Saved response.
    private java.util.Map<String,java.util.List<String>> responseHeader = null;
    private java.net.URL responseURL = null;
    private int responseCode = -1;
    private String MIMEtype  = null;
    private String charset   = null;
    private Object content   = null;
 
    /** Open a web file. */
    public WebFile( String urlString )
        throws java.net.MalformedURLException, java.io.IOException {
        // Open a URL connection.
        final java.net.URL url = new java.net.URL( urlString );
        final java.net.URLConnection uconn = url.openConnection( );
        if ( !(uconn instanceof java.net.HttpURLConnection) )
            throw new java.lang.IllegalArgumentException(
                "URL protocol must be HTTP." );
        final java.net.HttpURLConnection conn =
            (java.net.HttpURLConnection)uconn;
 
        // Set up a request.
        conn.setConnectTimeout( 10000 );    // 10 sec
        conn.setReadTimeout( 10000 );       // 10 sec
        conn.setInstanceFollowRedirects( true );
        conn.setRequestProperty( "User-agent", "spider" );
 
        // Send the request.
        conn.connect( );
 
        // Get the response.
        responseHeader    = conn.getHeaderFields( );
        responseCode      = conn.getResponseCode( );
        responseURL       = conn.getURL( );
        final int length  = conn.getContentLength( );
        final String type = conn.getContentType( );
        if ( type != null ) {
            final String[] parts = type.split( ";" );
            MIMEtype = parts[0].trim( );
            for ( int i = 1; i < parts.length && charset == null; i++ ) {
                final String t  = parts[i].trim( );
                final int index = t.toLowerCase( ).indexOf( "charset=" );
                if ( index != -1 )
                    charset = t.substring( index+8 );
            }
        }
 
        // Get the content.
        final java.io.InputStream stream = conn.getErrorStream( );
        if ( stream != null )
            content = readStream( length, stream );
        else if ( (content = conn.getContent( )) != null &&
            content instanceof java.io.InputStream )
            content = readStream( length, (java.io.InputStream)content );
        conn.disconnect( );
    }
 
    /** Read stream bytes and transcode. */
    private Object readStream( int length, java.io.InputStream stream )
        throws java.io.IOException {
        final int buflen = Math.max( 1024, Math.max( length, stream.available() ) );
        byte[] buf   = new byte[buflen];;
        byte[] bytes = null;
 
        for ( int nRead = stream.read(buf); nRead != -1; nRead = stream.read(buf) ) {
            if ( bytes == null ) {
                bytes = buf;
                buf   = new byte[buflen];
                continue;
            }
            final byte[] newBytes = new byte[ bytes.length + nRead ];
            System.arraycopy( bytes, 0, newBytes, 0, bytes.length );
            System.arraycopy( buf, 0, newBytes, bytes.length, nRead );
            bytes = newBytes;
        }
 
        if ( charset == null )
            return bytes;
        try {
            return new String( bytes, charset );
        }
        catch ( java.io.UnsupportedEncodingException e ) { }
        return bytes;
    }
 
    /** Get the content. */
    public Object getContent( ) {
        return content;
    }
 
    /** Get the response code. */
    public int getResponseCode( ) {
        return responseCode;
    }
 
    /** Get the response header. */
    public java.util.Map<String,java.util.List<String>> getHeaderFields( ) {
        return responseHeader;
    }
 
    /** Get the URL of the received page. */
    public java.net.URL getURL( ) {
        return responseURL;
    }
 
    /** Get the MIME type. */
    public String getMIMEType( ) {
        return MIMEtype;
    }
};
Bij voorbaat dank.
 
Laatst bewerkt door een moderator:
Code:
WebFile file = new WebFile( "http://voorbeeld.com" );
String MIME    = file.getMIMEType( );
Object content = file.getContent( );
if ( MIME.equals( "text/html" ) && content instanceof String )
{
    document.page.html.value = (String)content;
    
}

Moet na public class... komen..
 
ff editten want ik zat fout, maar het heeft idd te maken met de verkeerde positie van je eerste lijnen.
 
Laatst bewerkt:
Oké, dit werkt (er staat wel een deeltje in comment want ik zit achter een proxyserver)

PHP:
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Properties;
//import java.net.Authenticator;
//import java.net.PasswordAuthentication;


public final class WebFile {

		public static void main(String[] args){
			try {
				WebFile file = new WebFile( "http://www.google.com" );
				String MIME    = file.getMIMEType( );
				System.out.println("1: "+MIME);
				Object content = file.getContent( );
				System.out.println("2: "+content.toString());
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	    // Saved response.
	    private java.util.Map<String,java.util.List<String>> responseHeader = null;
	    private java.net.URL responseURL = null;
	    private int responseCode = -1;
	    private String MIMEtype  = null;
	    private String charset   = null;
	    private Object content = null;
	    // private String username = "**********";
	    // private String password = "**********";

	    /** Open a web file. */
	    public WebFile( String urlString )
	        throws java.net.MalformedURLException, java.io.IOException {
	        // Open a URL connection.
	        final java.net.URL url = new java.net.URL( urlString );
	        final java.net.URLConnection uconn = url.openConnection( );
	        if ( !(uconn instanceof java.net.HttpURLConnection) )
	            throw new java.lang.IllegalArgumentException(
	                "URL protocol must be HTTP." );

	        // Authenticator.setDefault(new SimpleAuthenticator(username,password));

	        // Properties systemProperties = System.getProperties();
	        // systemProperties.setProperty("http.proxyHost","proxysrv");
	        // systemProperties.setProperty("http.proxyPort","8080");

	        final java.net.HttpURLConnection conn = (java.net.HttpURLConnection)uconn;

	        // Set up a request.
	        conn.setConnectTimeout( 60000 );    // 10 sec
	        conn.setReadTimeout( 60000 );       // 10 sec
	        conn.setInstanceFollowRedirects( true );
	        //conn.setRequestProperty( "User-agent", "spider" );

	        // Send the request.
	        conn.connect( );

	        // Get the response.
	        responseHeader    = conn.getHeaderFields( );
	        responseCode      = conn.getResponseCode( );
	        responseURL       = conn.getURL( );
	        final int length  = conn.getContentLength( );
	        final String type = conn.getContentType( );
	        if ( type != null ) {
	            final String[] parts = type.split( ";" );
	            MIMEtype = parts[0].trim( );
	            for ( int i = 1; i < parts.length && charset == null; i++ ) {
	                final String t  = parts[i].trim( );
	                final int index = t.toLowerCase( ).indexOf( "charset=" );
	                if ( index != -1 )
	                    charset = t.substring( index+8 );
	            }
	        }

	        // Get the content.
	        final java.io.InputStream stream = conn.getErrorStream( );
	        if ( stream != null )
	            content = readStream( length, stream );
	        else if ( (content = conn.getContent( )) != null &&
	            content instanceof java.io.InputStream )
	            content = readStream( length, (java.io.InputStream)content );
	        conn.disconnect( );
	    }

	    /** Read stream bytes and transcode. */
	    private Object readStream( int length, java.io.InputStream stream )
	        throws java.io.IOException {
	        final int buflen = Math.max( 1024, Math.max( length, stream.available() ) );
	        byte[] buf   = new byte[buflen];;
	        byte[] bytes = null;

	        for ( int nRead = stream.read(buf); nRead != -1; nRead = stream.read(buf) ) {
	            if ( bytes == null ) {
	                bytes = buf;
	                buf   = new byte[buflen];
	                continue;
	            }
	            final byte[] newBytes = new byte[ bytes.length + nRead ];
	            System.arraycopy( bytes, 0, newBytes, 0, bytes.length );
	            System.arraycopy( buf, 0, newBytes, bytes.length, nRead );
	            bytes = newBytes;
	        }

	        if ( charset == null )
	            return bytes;
	        try {
	            return new String( bytes, charset );
	        }
	        catch ( java.io.UnsupportedEncodingException e ) { }
	        return bytes;
	    }

	    /** Get the content. */
	    public Object getContent( ) {
	        return content;
	    }

	    /** Get the response code. */
	    public int getResponseCode( ) {
	        return responseCode;
	    }

	    /** Get the response header. */
	    public java.util.Map<String,java.util.List<String>> getHeaderFields( ) {
	        return responseHeader;
	    }

	    /** Get the URL of the received page. */
	    public java.net.URL getURL( ) {
	        return responseURL;
	    }

	    /** Get the MIME type. */
	    public String getMIMEType( ) {
	        return MIMEtype;
	    }

//	 public class SimpleAuthenticator extends Authenticator {
//	    private String username,
//	                   password;
//
//	    public SimpleAuthenticator(String username,String password)
//	    {
//	       this.username = username;
//	       this.password = password;
//	    }
//
//	    protected PasswordAuthentication getPasswordAuthentication() {
//	       return new PasswordAuthentication(
//	              username,password.toCharArray());
//	    }
//	 }

}
 
Laatst bewerkt:
Bedankt voor jullie antwoorden, ik had het ook in die volgorde geprobeerd.

de laats geposte code heb ik zojuist geprobeerd maar ook daar krijg ik een foutmelding:

Foutdetails webpagina

Gebruikersagent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; GTB5; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; eSobiSubscriber 2.0.4.16; .NET CLR 3.0.30618; InfoPath.2; .NET CLR 3.5.30729)
Tijdstempel: Tue, 26 May 2009 20:18:47 UTC


Bericht: Syntaxisfout
Regel: 19
Teken: 1
Code: 0
URI: http://195.240.220.145:81/content/functions/get-page.php



op die pagina staat het script zoals aangegeven, heb je misschien nog een idee?
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan