⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 probe.java

📁 How to get the java home dir.
💻 JAVA
字号:
package com.mindprod.http;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;

/**
 * simulates a browser posting a form to CGI via GET.
 * It does not read any data. It is used for link checking where the server ignores HEAD requests.
 *
 * @author Roedy Green, Canadian Mind Products may be copied and used freely for any purpose but military.
 * @version 1.9 2008-08-22 support accept-charset, accept-encoding and accept-language.  Fix bugs in gzip support.
 */
@SuppressWarnings( { "WeakerAccess" } )
public final class Probe extends Http
    {
    // -------------------------- PUBLIC INSTANCE  METHODS --------------------------
    /**
     * constructor
     */
    public Probe()
        {
        }

    /**
     * Check if a URL is working. Does a HEAD probe, if that fails does a GET probe in case server is ignoring HEAD requests.
     * We don't pay any attention to the content.
     *
     * @param url complete URL including get parm,   only http:
     * @return responseCode
     */
    @SuppressWarnings( { "UnusedAssignment", "MethodNamesDifferingOnlyByCase" } )
    public int probe( URL url )
        {
        try
            {
            // defaults
            responseCode = DEFAULT_RESPONSE_CODE;
            responseMessage = DEFAULT_RESPONSE_MESSAGE;

            // O P E N

            HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection();
            urlc.setAllowUserInteraction( false );
            urlc.setDoInput( true );
            urlc.setDoOutput( false );// nothing beyond original request
            urlc.setUseCaches( false );

            // use a head first, it is the official way.
            urlc.setRequestMethod( "HEAD" );
            setStandardProperties( urlc );

            urlc.connect();
            /* save responseCode for later retrieval */
            responseCode = urlc.getResponseCode();
            responseMessage = urlc.getResponseMessage();
            urlc.disconnect();

            if ( responseCode == 200 )
                {
                return responseCode;
                }

            // HEAD failed. It may be because the server is being a jerk and ignoring HEADs. Try a GET.
            // get a fresh connection
            urlc = ( HttpURLConnection ) url.openConnection();
            urlc.setAllowUserInteraction( false );
            urlc.setDoInput( true );
            urlc.setDoOutput( false );// nothing beyond original request
            urlc.setUseCaches( false );

            // use a head first, it is the official way.
            urlc.setRequestMethod( "GET" );
            setStandardProperties( urlc );
            urlc.connect();
            /* save responseCode for later retrieval */
            responseCode = urlc.getResponseCode();
            responseMessage = urlc.getResponseMessage();
            // we need to be polite and dispose of the InputStream, even if we don't read the response.
            final InputStream is = urlc.getInputStream();
            // C L O S E
            is.close();
            urlc.disconnect();
            return responseCode;
            }
        catch ( ClassCastException e )
            {
            // was not an http: url
            return responseCode;
            }
        catch ( IOException e )
            {
            return responseCode;
            }
        }// end probe

    /**
     * Send a form full of data to the CGI host using GET.
     *
     * @param host   host name of the website, Should be form:"www.mindprod.com", no lead http://.
     * @param port   -1 if default, 8081 for local echoserver.
     * @param action action of form, page on website. Usually has a lead /.
     * @param parms  alternating parm value without = or ? not urlEncoded.
     * @return responseCode
     */
    @SuppressWarnings( { "UnusedAssignment", "MethodNamesDifferingOnlyByCase" } )
    public int probe( String host,
                      int port,
                      String action,
                      String... parms )
        {
        try
            {
            // defaults
            responseCode = DEFAULT_RESPONSE_CODE;
            responseMessage = DEFAULT_RESPONSE_MESSAGE;

            // O P E N
            final StringBuilder sb = new StringBuilder( 200 );
            for ( int i = 0; i < parms.length - 1; i += 2 )
                {
                if ( i != 0 )
                    {
                    sb.append( "&" );
                    }
                sb.append( URLEncoder.encode( parms[ i ], "UTF-8"
                        /* encoding */ ) );
                sb.append( '=' );
                sb.append( URLEncoder.encode( parms[ i + 1 ], "UTF-8"
                        /* encoding */ ) );
                }

            final String encodedParms = sb.toString();
            // O P E N

            // URL will encode target and parms.
            URL url = new URI( "http",
                    null,
                    host,
                    port,
                    action,
                    null
                    /* no query here, or would be double encoded */,
                    null ).toURL();
            url = new URL( url.toString() + '?' + encodedParms );

            return probe( url );
            }
        catch ( URISyntaxException e )
            {
            return responseCode;
            }
        catch ( IOException e )
            {
            return responseCode;
            }
        }// end probe
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -