/*
包  名 : 无
类  名 : PostXml.java
功  能 : 将数据发送到指定的URL
程  序 : MagicLiao
修 改 日 期 : 2003-03-26
*/
import java.lang.*;
import java.net.*;
import java.io.*; public class PostXML {

private final int MAX_PARA=255; //最大参数个数
private String AUTH_UID="anonymouse"; //认证用户名
private String AUTH_PWD="anonymouse"; //认证密码

private String XmlFileName=null; //读入的XML文件名
private String []ParaName=null; //传送参数名列表
private String []ParaValue=null; //传送内容列表
private int ParaCounter=0; //参数计数器

//构造方法1
public PostXML() {
this(null);
}

/*构造方法2
//入口参数:
FileName : 指定要读取的文件名
*/

public PostXML(String FileName) {
//得到文件名
this.XmlFileName=FileName;
//初始化Vector
this.ParaName=new String[this.MAX_PARA];
this.ParaValue=new String[this.MAX_PARA];
}

//设置认证用户名与密码
public void setAuthenticInfo(String UID,String PWD) {
this.AUTH_UID=UID; //认证用户名
this.AUTH_PWD=PWD; //认证密码
}

//设置要读取文件的文件名
public void setFileName( String FileName ) {
this.XmlFileName=FileName;
}

//获取要读取文件的文件名
public String getFileName() {
return this.XmlFileName;
}

//读文件内容
//返回文件内容
public String getXmlContent() throws Exception {
String strResult=null;
try {
strResult=this.getXmlContent(this.XmlFileName);
}catch(Exception Error) {
throw Error;
 }
 return strResult;
}

//读文件内容
public String getXmlContent(String FileName) throws Exception {
FileReader Fr=null;
BufferedReader Br=null;
StringBuffer strBuf=null;
String strLine=null;
strBuf=new StringBuffer();
try {
Fr=new FileReader(new File(FileName));
Br=new BufferedReader(Fr);
while( (strLine=Br.readLine())!=null )
strBuf.append(strLine+"\n");
}catch(Exception Error) {
throw Error;
 }
 return strBuf.toString();
}

/*附加参数
//入口参数:
// strName : POST数据的字段名称
// strValue : POST数据内容
*/

public void addParaValue( String strName,String strValue ) {
ParaName[this.ParaCounter]=strName;
ParaValue[this.ParaCounter]=strValue;
this.ParaCounter++;
}

//利用HTTP发送信息
//发送一无名称的数据
public String PostContent( String strUrl ) throws Exception {
String strResult=null;
try {
strResult=SendContent( "POST",strUrl );
}catch(Exception Error) {
throw Error;
 }
 return strResult;
}

//发送数据
private String SendContent( String strMethod , String strUrl ) throws Exception {
URL hostUrl=null; //URL
HttpURLConnection urlConn=null; //HTTP连接对象
PrintWriter streamOutput=null; //传送数据
BufferedReader Br=null; //获取应答数据
StringBuffer strBuf=null; //存放应答信息
StringBuffer strParaBuf=null; //存放参数信息
String strSendContent=null; //存放发送信息
String strLine=null; //流信息行存储

try {
//建立代理服务器连接
//System.getProperties().put("proxySet","true");
//System.getProperties().put("proxyHost","10.161.32.26");
//System.getProperties().put("proxyPort","8080");
//对远程连接进行认证
Authenticator.setDefault(new MyAuthenticator(this.AUTH_UID,this.AUTH_PWD));
//初始化各对象
strBuf=new StringBuffer();
strParaBuf=new StringBuffer();
hostUrl=new URL(strUrl); 
urlConn=(HttpURLConnection) hostUrl.openConnection();
//设置传送方法
urlConn.setRequestMethod(strMethod);
//设置读WEB应答信息
urlConn.setDoOutput(true);

streamOutput=new PrintWriter( urlConn.getOutputStream() );


for(int i=0;i<this.ParaCounter;i++) {
if( this.ParaName[i].equals("") )
throw new Exception("传值错误!!!");
else
if(i==0)
strParaBuf.append( this.ParaName[i]+"="+URLEncoder.encode( this.ParaValue[i],"gb2312") );
else
strParaBuf.append( "&"+this.ParaName[i]+"="+URLEncoder.encode( this.ParaValue[i],"gb2312") );
}

strSendContent=strParaBuf.toString();
//发送信息到指定URL
streamOutput.println(strSendContent);
streamOutput.close();
//获取访问指定URL后返回的信息
Br=new BufferedReader( new InputStreamReader( urlConn.getInputStream() ) );
while( (strLine=Br.readLine())!=null ) {
strBuf.append(strLine+"\n");
}
Br.close();
urlConn.disconnect();
} catch( Exception Error ) {
throw Error;
 }
 return strBuf.toString();
}

//主程序(测试用)
public static void main(String[] argv) {
PostXML oInstance=null;
String strResult=null;
String strUrl=null;
if(argv.length>0)
strUrl=argv[0];
else
strUrl="http://10.*.*.*/test/rform.asp";
oInstance=new PostXML("Attemper.XML");

try {
oInstance.addParaValue("XML",oInstance.getXmlContent());
oInstance.setAuthenticInfo("*****","******");
strResult=oInstance.PostContent( strUrl );
System.out.println("HTTP返回值:\n"+strResult);
} catch(Exception Error) {
System.out.println(Error.toString());
Error.printStackTrace();
 }
}
}
//~