分享
 
 
 

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

王朝other·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

作者:sonymusic

email: sonymusic@china.com

日期:2001-5-11 13:55:13

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="/resources/classes/Acme/PostURLConnection.java">Fetch the software.</A><BR>

// <A HREF="/resources/classes/Acme.tar.gz">Fetch the entire Acme package.</A>

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;

return (String) resHeaderValues.elementAt( i );

}

/// Gets a header field by name. Returns null if not known.

// The field is parsed as an integer.

// @param name the name of the header field

// @param def the value to return if the field is missing or malformed.

public int getHeaderFieldInt( String name, int def ) throws IOException

{

try

{

return Integer.parseInt( getHeaderField( name ) );

}

catch ( NumberFormatException t )

{

return def;

}

}

/// Gets a header field by name. Returns null if not known.

// The field is parsed as a date.

// @param name the name of the header field

// @param def the value to return if the field is missing or malformed.

public long getHeaderFieldDate( String name, long def ) throws IOException

{

try

{

return DateFormat.getDateInstance().parse( getHeaderField( name ) ).getTime();

}

catch ( ParseException e )

{

throw new IOException( e.toString() );

}

}

/// Call this routine to get an InputStream that reads from the object.

// @exception UnknownServiceException If the protocol does not support input.

public InputStream getInputStream() throws IOException

{

if ( ! doInput )

throw new UnknownServiceException(

"connection doesn´t support input" );

startInput();

return in;

}

/// Call this routine to get an OutputStream that writes to the object.

// @exception UnknownServiceException If the protocol does not support output.

public OutputStream getOutputStream() throws IOException

{

if ( ! doOutput )

throw new UnknownServiceException(

"connection doesn´t support output" );

connect();

return out;

}

/// Returns the String representation of the URL connection.

public String toString()

{

return this.getClass().getName() + ":" + url;

}

/// A URL connection can be used for input and/or output. Set the DoInput

// flag to true if you intend to use the URL connection for input,

// false if not. The default for PostURLConnections is false.

public void setDoInput( boolean doInput )

{

if ( connected )

throw new IllegalAccessError( "already connected" );

this.doInput = doInput;

}

public boolean getDoInput()

{

return doInput;

}

/// A URL connection can be used for input and/or output. Set the DoOutput

// flag to true if you intend to use the URL connection for output,

// false if not. The default for PostURLConnections is true.

public void setDoOutput( boolean doOutput )

{

if ( connected )

throw new IllegalAccessError( "already connected" );

this.doOutput = doOutput;

}

public boolean getDoOutput()

{

return doOutput;

}

/// Some protocols do caching of documents. Occasionally, it is important

// to be able to "tunnel through" and ignore the caches (e.g. the "reload"

// button in a browser). If the UseCaches flag on a connection is true,

// the connection is allowed to use whatever caches it can. If false,

// caches are to be ignored. The default for PostURLConnections is false.

public void setUseCaches( boolean useCaches )

{

if ( connected )

throw new IllegalAccessError( "already connected" );

this.useCaches = useCaches;

}

public boolean getUseCaches()

{

return useCaches;

}

// Sets/gets a general request property.

// @param name The keyWord by which the request is known (eg "accept")

// @param value The value associated with it.

public void setRequestProperty( String name, String value )

{

if ( connected )

throw new IllegalAccessError("already connected");

reqHeaderNames.addElement( name );

reqHeaderValues.addElement( value );

}

public String getRequestProperty( String name )

{

if ( connected )

throw new IllegalAccessError( "already connected" );

int i = reqHeaderNames.indexOf( name );

if ( i == -1 )

return null;

return (String) reqHeaderValues.elementAt( i );

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有