服务器:
1.jsp
<body>
<form name="_ctl0" method="post" action="TestFileManager.aspx" id="_ctl0" enctype="multipart/form-data">
<input type="hidden" name="__VIEWSTATE" value="dDwyNTIzNjA5NDU7Oz7rsE3eBYzQHDVtl+aTn96MvQW6PQ==" />
<p>
<input name="uploadfile1" id="uploadfile1" type="file" size="49" />
<input type="submit" name="Button1" value="?" id="Button1" />
</p>
<p>
<span id="Label1" style="width:459px;"></span>
</p>
<!-- Insert content here -->
</form>
</body>
客户端:
首先创建一个到服务器http的请求
HttpRequest request = new HttpRequest("http://服务器/1.jsp");
第一次使用的是GET方式
request.setMethod("GET");
紧接着进行一些请求的属性设置
request.setRequestHeader("Cache-Control", "no-cache");
这里保持连接,因为后面还要发送数据到服务器呢
request.setRequestHeader("Connection", "Keep-Alive");
下面是一些无关紧要的属性设置了。
request.setRequestHeader("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
request.setRequestHeader("Accept-Encoding", "gzip, deflate");
request.setRequestHeader("Accept-Language", "en-au");
request.setRequestHeader("Referer", "http://服务器/1.jsp");
request.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3215; .NET CLR 1.0.3705)");
构造好了连接请求,然后连接
request.connect();
紧接着提取Cookie值,在后文的post中可以用到。
String strCookie = request.getResponseHeader("Set-Cookie");
strCookie = strCookie.substring(0,strCookie.indexOf(";"));
下面通过循环查找,提取__VIEWSTATE的值
for ( int i = 0; i < nlist.getLength(); i++) {
node = nlist.item(i);
strName = getNodeAttributeValue(node,"name");
if ( strName.equals("__VIEWSTATE") ) {
strValue = getNodeAttributeValue(node,"value");
break;
}
}
往服务器组织发送数据
DataOutputStream dos = new DataOutputStream(request.getOutputStream());
dos.writeBytes("-----------------------------"+strBoundary);//这是每个要被发送数据间的间隔
dos.writeBytes(" Content-Disposition: form-data; name="__VIEWSTATE"");
dos.writeBytes(" "+strValue);
dos.writeBytes(" -----------------------------"+strBoundary);
这里面是发送文件的部分
dos.writeBytes(" Content-Disposition: form-data; name="uploadfile1"; filename="" + strFileName + """);
dos.writeBytes(" Content-Type: text/xml");
dos.writeBytes(" ");
dos.writeBytes(new String(data));
dos.writeBytes(" -----------------------------"+strBoundary);
dos.writeBytes(" Content-Disposition: form-data; name="Button1"");
dos.writeBytes(" 上传");
dos.writeBytes(" -----------------------------"+strBoundary+"--");
dos.writeBytes(" ");
dos.close();
HttpClient 用post方法提交数据 源代码:
package post;
import java.io.IOException;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
public class PostHttp {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HttpClient httpClient = new HttpClient();
String url = "http://193.167.13.21/setup.cgi";
PostMethod postMethod = new PostMethod(url);
// 填入各个表单域的值
NameValuePair[] data = {
new NameValuePair("ID", "11"),
new NameValuePair("mtg", "0"),
new NameValuePair("haveCookie", "0"),
new NameValuePair("backID", "30"),
new NameValuePair("psw", "password")
};
// 将表单的值放入postMethod中
postMethod.setRequestBody(data);
// 执行postMethod
int statusCode = 0;
try {
statusCode = httpClient.executeMethod(postMethod);
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发
// 301或者302
if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY)
{
// 从头中取出转向的地址
Header locationHeader = postMethod.getResponseHeader("location");
String location = null;
if (locationHeader != null) {
location = locationHeader.getValue();
System.out.println("The page was redirected to:" + location);
}
else {
System.err.println("Location field value is null.");
}
return;
}
else
{
System.out.println(postMethod.getStatusLine());
String str = "";
try {
str = postMethod.getResponseBodyAsString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(str);
}
postMethod.releaseConnection();
return ;
}
}
//这是一个用户认证过程的登陆信息
其中需要的jar包:
1、 commons-httpclient-3.1-rc1.zip http://jakarta.apache.org/commons/httpclient/downloads.html
2、commons-codec-1.3.jar http://jakarta.apache.org/site/downloads/downloads_commons-codec.cgi
3、commons-logging-api.jar 在tomcat5.5中的Tomcat 5.5\bin目录下或者 http://jakarta.apache.org/site/downloads/downloads_commons-logging.cgi
方法3:
1.用HttpURLConnection 类
2.用connect建连接
3.用setRequestMethod 方法设置request方法(post,get..)
4.用getResponseMessage 方法取内容
我没有试过,具体请看apiTop
不用模拟,这个类本来就提供了这样的接口的。
如上所述。(不过我也没用过。:) )Top
错了,我只在 J2ME 的手机里面用过这个类 post 过数据(不过已经是经过改装过的了)。Top
参见本版关于servlet与applet的通信过程Top
gzTop
有用过的吗?请给出例子。
其实get方法我已经实现,以下代码能够出来结果?请给出正确的Post到一个表单的方法。
URL destURL = new URL("http://192.168.68.234:8100/form_test.jsp");
HttpURLConnection urlConn= (HttpURLConnection)destURL.openConnection();
System.out.println(urlConn.getContent());
System.out.println(urlConn.getRequestMethod());
System.out.println(""+urlConn.getResponseCode());
/*DataInputStream inStream = new DataInputStream(
urlConn.getInputStream());
int ch;
//display
while ((ch = inStream.read()) >= 0) {
System.out.print((char) ch);}Top
给你发到信箱里了。Top
http://www.jscape.com/jhttp.htmlTop
public InputStream sendPostMessage(Properties args1)
throws IOException
{
String argString = "";
if (args1 != null)
{
argString = toEncodedString(args1);
}
URLConnection con = servlet.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
DataOutputStream out = new DataOutputStream(con.getOutputStream());
out.writeBytes(argString);
out.flush();
out.close();
return con.getInputStream();
}
试试,比较好用。