package com.pinyougou.httpclient.login;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* Created by xiaohuihui on 2018-05-12
*/
public static void main(String[] args) {
String respStr = null;
// 1. 登录pinyougou,同时存入cookie
// HttpClient httpClient = getCookie("http://localhost:9102/login");
TestHttpClient testHttpClient = new TestHttpClient();
CloseableHttpClient httpClient = null;
try {
httpClient = testHttpClient.testLogin();
} catch (Exception e) {
e.printStackTrace();
}
// 2. 使用HttpClient完成文件上传
// String afterLoginUrl = "http://localhost:9102/itemCat/findByParentId.do?parentId=1";
// afterLogin(httpClient, afterLoginUrl);
// 3. 使用HttpClient完成文件上传
String uploadUrl = "http://localhost:9102/upload.do";
HttpPost httpPost = new HttpPost(uploadUrl);
FileBody fileBody = new FileBody(new File("C:/Users/Administrator/Desktop/timg.jpg"));
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder.addPart("file",fileBody);
// 设置其他参数
List nvps = new ArrayList();
nvps.add(new NameValuePair("Accept","application/json, text/plain, */*"));
nvps.add(new NameValuePair("Accept-Encoding","gzip, deflate, br"));
nvps.add(new NameValuePair("Accept-Language","zh-CN,zh;q=0.9"));
nvps.add(new NameValuePair("Connection","keep-alive"));
nvps.add(new NameValuePair("Content-Length","28700"));
nvps.add(new NameValuePair("Content-Type","multipart/form-data; boundary=----WebKitFormBoundarypaEfQmIQBbUrkI0c"));
nvps.add(new NameValuePair("Host","localhost:9102"));
nvps.add(new NameValuePair("Origin","http://localhost:9102"));
nvps.add(new NameValuePair("Referer","http://localhost:9102/admin/goods_edit.html"));
nvps.add(new NameValuePair("User-Agent","Mozilla/5.0 (Windows NT 6.1; WO