页面还是要吧,就是要有个运行的页面,如下面这样 html+javascript来post form
<script language=JavaScript>
function DoSendToClient() {
//document.toClientWithReturnWindow.action='http://epay.net.cn/epay/getFromEpay.aspx';
document.alogon.submit();
}
</script>
<body onload="DoSendToClient()">
<table width="68%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="#004d99" width="20" height="25"></td>
<form method="POST" action='http://expert.csdn.net/member/logon.asp' name="alogon" >
<td bgcolor="#004d99"><font color="#FFFFFF">姓 名:</font> <input name="name" type="text" class=FormText_1 value="xuu27" size="7">
<font color="#FFFFFF">密 码:
<input name="pass" class=FormText_1 type="password" value="******" size="7">
</font> <select name="type" class=FormText_1>
<option value="1" selected>我的论坛</option>
<option value="2">我的软件</option>
<option value="3">我的订单</option>
<option value="4">我的文档</option>
<option value="5">我的简历</option>
</select> <INPUT TYPE="image" width=0 height=0> <input type="submit" name="Submit" value="登 陆">
</td>
</form>
</tr>
</table>
</body>
<script language=JavaScript>
function DoSendToClient() {
//document.toClientWithReturnWindow.action='http://epay.net.cn/epay/getFromEpay.aspx';
document.alogon.submit();
}
</script>
<body onload="DoSendToClient()">
<table width="68%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="#004d99" width="20" height="25"></td>
<form method="POST" action='http://expert.csdn.net/member/logon.asp' name="alogon" >
<td bgcolor="#004d99"><font color="#FFFFFF">姓 名:</font> <input name="name" type="text" class=FormText_1 value="xuu27" size="7">
<font color="#FFFFFF">密 码:
<input name="pass" class=FormText_1 type="password" value="******" size="7">
</font> <select name="type" class=FormText_1>
<option value="1" selected>我的论坛</option>
<option value="2">我的软件</option>
<option value="3">我的订单</option>
<option value="4">我的文档</option>
<option value="5">我的简历</option>
</select> <INPUT TYPE="image" width=0 height=0> <input type="submit" name="Submit" value="登 陆">
</td>
</form>
</tr>
</table>
</body>
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;public class SimulateUpload
{
private static final byte[] BOUNDARY = "---------------------------7d33bc1b47038c".getBytes();
private static final byte[] CRLF = "\r\n".getBytes(); public void simUpload(String page, String[] paramNames, Object[] paramValues, OutputStream os) throws IOException
{
int contentLength = calculateLength(paramNames, paramValues); os.write(("POST " + page + " HTTP/1.1").getBytes());
os.write(CRLF); os.write("User-Agent: myselfHttp/1.0".getBytes());
os.write(CRLF); os.write("Accept: */*".getBytes());
os.write(CRLF); os.write("Host: ".getBytes());
os.write(InetAddress.getLocalHost().getHostAddress().getBytes());
os.write(CRLF); os.write("Content-Type: multipart/form-data; boundary=".getBytes());
os.write(BOUNDARY);
os.write(CRLF); os.write(("Content-Length: " + contentLength).getBytes());
os.write(CRLF); os.write(CRLF); os.write(BOUNDARY);
os.write(CRLF);
for(int i=0; i<paramNames.length; i++)
{
if(paramValues[i] instanceof String)
writeParameter(os, paramNames[i], (String) paramValues[i]);
else if(paramValues[i] instanceof File)
writeFile(os, paramNames[i], (File) paramValues[i]);
}
os.flush();
} private void writeFile(OutputStream os, String paramName, File file) throws IOException
{
os.write("Content-Disposition: form-data; name=\"".getBytes());
os.write(paramName.getBytes());
os.write("\"; filename=\"".getBytes());
os.write(file.getAbsolutePath().getBytes());
os.write("\"\r\nContent-Type: application/octet-stream".getBytes());
os.write(CRLF);
os.write(CRLF); FileInputStream fis = new FileInputStream(file);
byte[] buf = new byte[4096];
int bytes = 0;
while((bytes = fis.read(buf)) != -1)
os.write(buf, 0, bytes);
fis.close(); os.write(CRLF);
os.write(BOUNDARY);
os.write(CRLF);
} private void writeParameter(OutputStream os, String name, String value) throws IOException
{
os.write("Content-Disposition: form-data; name=\"".getBytes());
os.write(name.getBytes());
os.write('"');
os.write(CRLF);
os.write(CRLF);
os.write(value.getBytes());
os.write(CRLF);
os.write(BOUNDARY);
os.write(CRLF);
} private int calculateLength(String[] paramNames, Object[] paramValues)
{
int result = BOUNDARY.length + CRLF.length;
for(int i=0; i<paramNames.length; i++)
{
if(paramValues[i] instanceof String)
result += 88 + paramNames[i].getBytes().length + ((String)paramValues[i]).getBytes().length;
else if(paramValues[i] instanceof File)
{
File file = (File) paramValues[i];
result += 141 + paramNames[i].getBytes().length + file.getAbsolutePath().getBytes().length + file.length();
}
}
return result;
} public static void main(String[] args)
{
try
{
Socket socket = new Socket("localhost", 80);
OutputStream os = socket.getOutputStream();
SimulateUpload sim = new SimulateUpload();
String[] paramNames = new String[]{"param1", "param2", "param3", "file", "file2"};
Object[] paramValues = new Object[]{"value1", "value2", "value3", new File("d:\\test.txt"), new File("d:\\test1.txt")};
sim.simUpload("/uploadServlet", paramNames, paramValues, os);
os.flush();
os.close();
socket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
int port=80;
fileName="/news/cgi-bin/add.cgi";
String content="可以是name=value形式";
Socket sc = new Socket(host,port);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(sc.getOutputStream()));
String cmd = "POST "+fileNaem+" HTTP/1.0\r\n"+"User-Agent: myselfHttp/1.0\r\n"+
"Accept: www/source; text/html; image/gif; */*\r\n";
cmd += "Content-type: application/x-www-form-urlencoded\r\n";
cmd += "Content-length: " + content.length() + "\r\n\r\n";
cmd += content+"\r\n";
cmd += 要发送的数据;//注意最后要以\r\n结尾
pw.print(cmd);
pw.close();