ZhangLiHai.Com Blog


用URLConnection向服务器端程序用POST发送数据后返回结果

张利海 于 2003年05月26日 发表

以下只是个简单的实现原理,只有一个主函数。

import java.net.*;
import java.io.*;
/**
用POST 方式发送数据 然后从结果中读取
FileName:SocketURLConnection.java
@Author:Alec Cheung
MSN:eboysit@hotmail.com
Home:www.andyfans.com
http://97741.126.com
http://cheung.cxc.cc
Date:2003-5-23
*/

public class DoPostURLConnection
{
public static void main(String args[])
{
try{

URL url = new URL("http://localhost:8888/97741/815/formAction.asp");
URLConnection uc = url.openConnection();
uc.setDoOutput(true);

OutputStream raw = uc.getOutputStream();
OutputStream buf = new BufferedOutputStream(raw);
OutputStreamWriter out = new OutputStreamWriter(buf,"GBK");
out.write("myName=Alec Cheung&myEmail=eboysit@hotmail.com");
out.flush();
out.close();

InputStream in = uc.getInputStream();
in = new BufferedInputStream(in);
Reader r = new InputStreamReader(in);
int c;
System.out.println("==================Beging====================");
while((c = r.read()) != -1)
System.out.print((char) c);
in.close();
System.out.println("===================End======================");
}
catch(IOException e){
///
}
}
}


---------------------------------------------
本人学习笔记

新版本Blog中有更多内容
Copyright (C)2002-2007 All Rights Reserved Powered By:ZhangLiHai.Com