精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>● Java>>JAVA编程>>网络编程>>在applet 中如何用url模拟post发送多数量参数(技巧提示)

主题:在applet 中如何用url模拟post发送多数量参数(技巧提示)
发信人: lega(摇滚java)
整理人: zjxyz(2002-12-19 11:16:23), 站内信件
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Copyright (c) Non, Inc. 1997 -- All Rights Reserved

PROJECT: JavaWorld
MODULE: Web Stuff
FILE: Happy.java

AUTHOR: John D. Mitchell, Jul  8, 1997

REVISION HISTORY:
Name Date Description
---- ---- -----------
JDM 97.07.08    Initial version.

DESCRIPTION:

This file shows how to POST to a web-server and get back the raw
response data.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/


import java.awt.*;
import java.applet.*;
import java.io.*;
import java.util.*;
import java.net.*;


public class Happy extends Applet
    {
    private TextArea textArea = new TextArea (25, 70);

    public void init ()
{
try
    {
    URL url;
    URLConnection urlConn;
    DataOutputStream printout;
    DataInputStream input;

    // URL of CGI-Bin script.
    url = new URL (getCodeBase().toString() + "env.cgi");

    // URL connection channel.
    urlConn = url.openConnection();

    // Let the run-time system (RTS) know that we want input.
    urlConn.setDoInput (true);

    // Let the RTS know that we want to do output.
    urlConn.setDoOutput (true);

    // No caching, we want the real thing.
    urlConn.setUseCaches (false);

    // Specify the content type.
    urlConn.setRequestProperty
("Content-Type", "application/x-www-form-urlencoded");

    // Send POST output.
    printout = new DataOutputStream (urlConn.getOutputStream ());

    String content =
"name=" + URLEncoder.encode ("Buford Early") +
"&email=" + URLEncoder.encode ("[email protected]");
    
    printout.writeBytes (content);
    printout.flush ();
    printout.close ();

    // Get response data.
    input = new DataInputStream (urlConn.getInputStream ());

    String str;
    while (null != ((str = input.readLine())))
{
System.out.println (str);
textArea.appendText (str + "\n");
}

    input.close ();

    // Display response.
    add ("Center", textArea);
    }
catch (MalformedURLException me)
    {
    System.err.println("MalformedURLException: " + me);
    }
catch (IOException ioe)
    {
    System.err.println("IOException: " + ioe.getMessage());
    }
} // End of method init().
    } // End of class Happy.




----
你说的每一句话,都将成为呈堂证供

[关闭][返回]