📄 submithttpform.java~2~
字号:
package org.apache.commons.httpclient.demo;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
public class SubmitHttpForm {
private static String url ="http://localhost:8080/validatorStrutsApp/userInfo.do";
public static void main(String[] args) {
//Instantiate an HttpClient
HttpClient client = new HttpClient();
//Instantiate a GET HTTP method
HttpMethod getmethod = new GetMethod(url);
PostMethod postmethod = new PostMethod("/search2005.php");
//Define name-value pairs to set into the QueryString
NameValuePair nvp1 = new NameValuePair("firstName", "fname");
NameValuePair nvp2 = new NameValuePair("lastName", "lname");
NameValuePair nvp3 = new NameValuePair("email", "email@email.com");
getmethod.setQueryString(new NameValuePair[] {nvp1, nvp2, nvp3});
try {
int statusCode = client.executeMethod(getmethod);
System.out.println("QueryString>>> " + getmethod.getQueryString());
System.out.println("Status Text>>> " + HttpStatus.getStatusText(statusCode));
//Get data as a String
System.out.println(getmethod.getResponseBodyAsString());
//OR as a byte array
byte[] res = getmethod.getResponseBody();
//write to file
FileOutputStream fos = new FileOutputStream("donepage.html");
fos.write(res);
//release connection
getmethod.releaseConnection();
} catch (IOException e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -