关于java模拟post 提交multipart/form-data的问题

dozhn 2016-04-08 11:08:55
通过fiddler抓包和提交都没有问题:

请求页面:http://103.247.176.245:8081/tool/syllabletag/
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryzM0nS9GIodjxCMih
post参数:
------WebKitFormBoundaryzM0nS9GIodjxCMih
Content-Disposition: form-data; name="csrfmiddlewaretoken"

z6l45zfflLQmzmkM2IYQwfTBSKCVUnOl
------WebKitFormBoundaryzM0nS9GIodjxCMih
Content-Disposition: form-data; name="input_text"

hahhahhahahahah
------WebKitFormBoundaryzM0nS9GIodjxCMih
Content-Disposition: form-data; name="output_text"


------WebKitFormBoundaryzM0nS9GIodjxCMih--



请求头:


请求参数



通过fiddler提交后的结果:




HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("http://103.247.176.245:8081/tool/syllabletag/");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
try {
reqEntity.addPart("csrfmiddlewaretoken", new StringBody(getPageToken()));
reqEntity.addPart("input_text", new StringBody("hahhahhahahahah"));
reqEntity.addPart("output_text", new StringBody(""));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

postRequest.setEntity(reqEntity);
HttpResponse response;
try {
response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();
while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


我通过httpclient模拟提交页面返回的403
...全文
12247 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
菩提丶道人 2020-04-10
  • 打赏
  • 举报
回复
00没人回复,那我回复一下,

String boundary = "----WebKitFormBoundarygmk3KBUQwymfMc8A";
		//请求地址
		String url = "https://url";
		String param = "";
		param +="--"+boundary+"\r\n";
		param += "Content-Disposition: form-data; name=\"account\""+"\r\n\r\n";
		//参数account
		param+= "account"+"\r\n";
		param +="--"+ boundary+"\r\n";
		param += "Content-Disposition: form-data; name=\"password\""+"\r\n\r\n";
		//参数password
		param += "password"+"\r\n";
		param+="--"+boundary+"--";
		//请求头,看情况自己加
		Map<String,String> headMap = new HashMap<>();
		headMap.put("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundarygmk3KBUQwymfMc8A");
		Map<String,Object> reMap = HttpClientUtil.doMultipartFormDataPost(url,param,headMap);

public static Map<String, Object> doMultipartFormDataPost(String url, String param, Map<String, String> headParam) {
		// TODO Auto-generated method stub

		// 创建Httpclient对象
		Map<String,Object> resultMap = new HashMap<>();
		String resultString = "";
		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		try {
			// 创建Http Post请求
			HttpPost httpPost = new HttpPost(url);
			// 创建请求内容
			StringEntity entity = new StringEntity(param, ContentType.MULTIPART_FORM_DATA);
			httpPost.setEntity(entity);
			for(String key:headParam.keySet()) {
				httpPost.addHeader(key,headParam.get(key));
			}
			// 执行http请求
			response = httpClient.execute(httpPost);
			resultString = EntityUtils.toString(response.getEntity(), "utf-8");
			Header[] headers =response.getHeaders("Set-Cookie");
			Map<String,String> headMap = new HashMap<>();
			for(int i=0;i<headers.length;i++) {
				headMap.put(headers[i].getElements()[0].getName(), headers[i].getElements()[0].getValue());	
			}
			resultMap.put("headMap", headMap);
			resultMap.put("result", resultString);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				response.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return resultMap;
	}

maven依赖

<dependency>
		<groupId>org.apache.httpcomponents</groupId>
		<artifactId>httpclient</artifactId>
</dependency>
tingjiazhang 2017-06-27
  • 打赏
  • 举报
回复
这个问题楼主有解决吗?能否代码贴出来一下 不胜感激啊 我也卡在这里了
爱睡觉的阿狸 2016-04-09
  • 打赏
  • 举报
回复
java相关板块人就是非常少。还有,too long no see,对不起。
dozhn 2016-04-09
  • 打赏
  • 举报
回复
我擦 没人呢
dozhn 2016-04-08
  • 打赏
  • 举报
回复
在线等。。。就是通过调用这个请求能够得到分词的效果,不胜感激

2,204

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 CGI
社区管理员
  • CGI社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧