第一种:
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
/**
* 用httpClient来模拟登录。
*/
public class HttpPostDemo {
public static void main(String[] args) {
String path="http://192.168.10.88:8080/MyServer/login";
HttpClient client=new DefaultHttpClient();
HttpPost post = new HttpPost(path);
//将数据封装到post对象里。
//创建一个存储封装键值对对象的集合
List<NameValuePair> parameters=new ArrayList<NameValuePair>();
//将数据封装到NameValuePair对象里,第一个参数为键,第二个参数为值
NameValuePair value1 = new BasicNameValuePair("username", "admin");
NameValuePair value2=new BasicNameValuePair("userpwd", "111");
//将对象添加到集合中。
parameters.add(value1);
parameters.add(value2);
HttpEntity entity = null;
try {
//将数据集合封装成HttpEntity
entity = new UrlEncodedFormEntity(parameters);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//设置HttpEntity
post.setEntity(entity);
//让客户端执行请求。
try {
HttpResponse response=client.execute(post);
int code=response.getStatusLine().getStatusCode();
if(code==200){
HttpEntity entity2=response.getEntity();
String str=EntityUtils.toString(entity2);
System.out.println(str);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
第二种:
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.FormBodyPart;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class HttpUpload {
public static void main(String[] args) {
HttpClient client = new DefaultHttpClient();
String path="http://localhost:8080/FileUpload/FileUploadServlet";
HttpPost post = new HttpPost(path);
//这里需要上传数据,所以需要改变表单form的enctyped属性值,改为mul/data-form
MultipartEntity entity=new MultipartEntity();
//封装文件
FileBody body=new FileBody(new File("e:\\login.jpeg"));
FormBodyPart pary=new FormBodyPart("form1", body);
//将封装文件的对象添加到MultipartEntity
entity.addPart(pary);
//添加其他的数据
try {
entity.addPart("username",new StringBody ("admin"));
entity.addPart("passwoed",new StringBody("123"));
//设置给HttpPost对象
post.setEntity(entity);
//执行请求
HttpResponse response=client.execute(post);
//得到响应码
int code=response.getStatusLine().getStatusCode();
if(code==200){
HttpEntity entity2=response.getEntity();
//EntityUtils.toByteArray(entity);
String str= EntityUtils.toString(entity2);
System.out.println(str);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
转载于:https://my.oschina.net/u/2541146/blog/596516