<%@ page contentType="text/html; charset=gb2312" %><%@ page language="java" import="java.sql.*,javax.servlet.*,javax.servlet.http.*,java.util.*,java.net.*,java.io.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'OpposiveSendXml.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
Send Xml to Server <br>
<%!
public String SendXml(String strPostUrl,String strXml,int nReadTimeOut ) throws Exception
{
URL url = new URL(strPostUrl);
URLConnection connection = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) connection;
OutputStream out = null;
String strTempRecXml = "";
DataInputStream dis = null;
try
{
byte[] b = strXml.getBytes();
httpConn.setConnectTimeout(10000);
httpConn.setReadTimeout(nReadTimeOut);
httpConn.setRequestProperty( "Content-Length",String.valueOf( b.length ) );
httpConn.setRequestProperty("Content-Type","text/xml; charset=gbk");
httpConn.setRequestMethod( "POST" );
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
out = httpConn.getOutputStream();
out.write( b );
out.close();
byte bRecByte[] = new byte[1025];
dis = new DataInputStream(httpConn.getInputStream());
BufferedInputStream bis= new BufferedInputStream(dis);
int nByte = -1;
ByteArrayOutputStream baoStream = new ByteArrayOutputStream();
nByte = bis.read(bRecByte,0,1024);
while( nByte != -1 )
{
baoStream.write(bRecByte,0,nByte);
nByte = bis.read(bRecByte,0,1024);
}
byte abyte[] = baoStream.toByteArray();
strTempRecXml = new String(abyte,"GBK");
strTempRecXml = strTempRecXml.replaceAll("<","<");
strTempRecXml = strTempRecXml.replaceAll(">",">");
}
catch(Exception e)
{
System.out.println("发送POST请求出现异常!" + e);
e.printStackTrace();
}
//使用finally块来关闭输入流
finally
{
try
{
if (out != null)
{
out.close();
out = null;
}
if (dis != null)
{
dis.close();
dis = null;
}
if (httpConn != null)
{
httpConn.disconnect();
httpConn = null;
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
return strTempRecXml;
}
%>
<form name="frm" action="testBossResponse.jsp" method="post">
<center>URL<input type="text" name="url" style=" 150px"/>
<br/>
时间:<input type="text" name="time" style=" 164px"/>
<br/>
<textarea name="testxml" rows="20" cols="50"></textarea>
<br/>
<input type="hidden" name="subTag" value="">
<input type="submit" value="提交" onclick="javascript:document.frm.subTag.value='sub'">
</center>
</form>
<%
String subtag = request.getParameter("subTag")==null?"":request.getParameter("subTag");
String msg = "";
if(subtag.equals("sub")){
String rs_url = request.getParameter("url");
String rs_time = request.getParameter("time");
String rs_value = request.getParameter("testxml");
msg = SendXml(rs_url,rs_value,Integer.parseInt(rs_time));
if(msg.length()>0)
{
out.println("结果\n:"+msg);
out.println("发送模拟的账户信息\n");
SendXml("http://localhost:8090/MobileAd/MainServers","<operation_out>"
+"<service_name>pcs_mobileadvertise_agent_information</service_name>"
+"<request_type>1001</request_type>"
+"<sysfunc_id>93001001</sysfunc_id>"
+"<request_seq>100000031186</request_seq>"
+"<response_time>20091013113712</response_time>"
+"<response_seq>101313103</response_seq>"
+"<request_source>201015</request_source>"
+"<response>"
+"<start_time>0</start_time>"
+"<brand_id>1012</brand_id>"
+"<total_amount>20</total_amount>"
+"</response>"
+"<content/>"
+"</operation_out>",10000);
}
else
{
out.println("结果\n:"+"无返回结果");
}
}
%>
</body>
</html>