当前位置:编程学习 > JSP >>

Acme包中的以Post方式发送数据的例子。

答案:package Acme;

import java.net.*;
import java.io.*;
import java.util.*;
import java.text.*;

/// A URLConnection that implements POST.
// <P>
// Some implementations of URLConnection, e.g. the one in Navigator 3.0,
// do not support POST.  This is a stripped-down version that does.
// <P>
// Note that it can't inherit from java.net.URLConnection because that
// class has no public constructors.  Not all the standard URLConnection
// methods are re-implemented here, just the ones necessary for posting.
// <P>
// This class is not needed in current browsers.
// <P>
// <A HREF=>// <A HREF=>
public class PostURLConnection
    {

    private URL url;
    private boolean doInput = false;
    private boolean doOutput = true;
    private boolean useCaches = false;

    private Vector reqHeaderNames = new Vector();
    private Vector reqHeaderValues = new Vector();
    private Vector resHeaderNames = null;
    private Vector resHeaderValues = null;
    private Socket socket;
    private OutputStream out;
    private InputStream in;


    /// Constructs a POST URL connection to the specified URL.
    // @param url the specified URL
    public PostURLConnection( URL url )
    {
    this.url = url;
    }


    private boolean connected = false;

    public void connect() throws IOException
    {
    if ( connected )
        return;
    if ( ! useCaches )
        setRequestProperty( "Pragma", "no-cache" );
    String protocol = url.getProtocol();
    if ( ! protocol.equals( "http" ) )
        throw new UnknownServiceException( "unknown protocol" );
    String host = url.getHost();
    int port = url.getPort();
    if ( port == -1 )
        port = 80;
    String file = url.getFile();
    socket = new Socket( host, port );
    out = socket.getOutputStream();
    PrintStream pout = new PrintStream( out );
    String method;
    if ( doOutput )
        method = "POST";
    else
        method = "GET";
    pout.println( method + " " + file + " HTTP/1.0" );
    for ( int i = 0; i < reqHeaderNames.size(); ++i )
        {
        String name = (String) reqHeaderNames.elementAt( i );
        String value = (String) reqHeaderValues.elementAt( i );
        pout.println( name + ": " + value );
        }
    pout.println( "" );
    pout.flush();
    connected = true;
    }
    

    private boolean inputStarted = false;

    private void startInput() throws IOException
    {
    connect();
    if ( inputStarted )
        return;
    in = socket.getInputStream();
    resHeaderNames = new Vector();
    resHeaderValues = new Vector();
    DataInputStream din = new DataInputStream( in );
    String line;
    // Read and ignore the status line.
    line = din.readLine();
    // Read and save the header lines.
    while ( true )
        {
        line = din.readLine();
        if ( line == null || line.length() == 0 )
        break;
        int colonBlank = line.indexOf( ": " );
        if ( colonBlank != -1 )
        {
        String name = line.substring( 0, colonBlank );
        String value = line.substring( colonBlank + 2 );
        resHeaderNames.addElement( name.toLowerCase() );
        resHeaderValues.addElement( value );
        }
        }
    inputStarted = true;
    }
    

    public void close() throws IOException
    {
    if ( ! connected )
        return;
    out.close();
    if ( inputStarted )
        in.close();
    socket.close();
    }


    /// Gets the URL for this connection.
    public URL getURL()
    {
    return url;
    }

    // Gets the content length.  Returns -1 if not known.
    public int getContentLength() throws IOException
        {
        return getHeaderFieldInt( "content-length", -1 );
    }

    /// Gets the content type.  Returns null if not known.
    public String getContentType() throws IOException
    {
    return getHeaderField( "content-type" );
    }

    /// Gets a header field by name.  Returns null if not known.
    // @param name the name of the header field
    public String getHeaderField( String name ) throws IOException
    {
    if ( resHeaderNames == null )
        startInput();
    int i = resHeaderNames.indexOf( name.toLowerCase() );
    if ( i == -1 )
        return null;

上一个:jsp或者说JAVA倒底有多快?这里有一个计时类,可以帮你的忙。同时支持JAVA和JSP。内有例子。
下一个:MD5算法(看在你是女孩的份上,给你一个吧)

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,