最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java模擬發(fā)送form-data的請求方式

 更新時間:2020年05月05日 17:23:44   作者:NPException  
這篇文章主要介紹了java模擬發(fā)送form-data的請求方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

我就廢話不多說了,大家還是直接看代碼吧!

package com.silot.test;
 
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
 
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
 
public class TestCli
{
  public static void main(String args[]) throws Exception
  {
    MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, "------------------------------0ea3fcae38ff", Charset.defaultCharset());
    multipartEntity.addPart("skey", new StringBody("哈哈哈哈哈", Charset.forName("UTF-8")));
    multipartEntity.addPart("operator", new StringBody("啦啦啦啦", Charset.forName("UTF-8")));
    multipartEntity.addPart("VrfKey", new StringBody("渣渣渣", Charset.forName("UTF-8")));
    multipartEntity.addPart("StatCode", new StringBody("00", Charset.forName("UTF-8")));
    multipartEntity.addPart("mass_id", new StringBody("1234", Charset.forName("UTF-8")));
    multipartEntity.addPart("reference_id", new StringBody("21231544", Charset.forName("UTF-8")));
 
    HttpPost request = new HttpPost("http://xiefei.s1.natapp.cc/v1/withdrawCallback");
    request.setEntity(multipartEntity);
    request.addHeader("Content-Type", "Content-Disposition: form-data; boundary=------------------------------0ea3fcae38ff");
 
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpResponse response = httpClient.execute(request);
 
    InputStream is = response.getEntity().getContent();
    BufferedReader in = new BufferedReader(new InputStreamReader(is));
    StringBuffer buffer = new StringBuffer();
    String line = "";
    while ((line = in.readLine()) != null)
    {
      buffer.append(line);
    }
 
    System.out.println("發(fā)送消息收到的返回:" + buffer.toString());
  }
}

補(bǔ)充知識:java模擬復(fù)雜表單post請求

java模擬復(fù)雜表單post請求

能支持文件上傳

/**
	 * 支持復(fù)雜表單,比如文件上傳
	 * @param formParam
	 * @return
	 * @throws Exception
	 */
	public static String postWithForm(FormParam formParam) throws Exception {
		String url = formParam.getUrl();
		String charset = "UTF-8";
		String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
		String CRLF = "\r\n"; // Line separator required by multipart/form-data.

		URLConnection connection = new URL(url).openConnection();
		connection.setDoOutput(true);
		connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
		try (
				OutputStream output = connection.getOutputStream();
				PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);
			) {
			// make body param
			Map<String, String> bodyParam = formParam.getBodyParam();
			if (null != bodyParam) {
				for (String p : bodyParam.keySet()) {
					writer.append("--" + boundary).append(CRLF);
					writer.append("Content-Disposition: form-data; name=\"" + p + "\"").append(CRLF);
					writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
					writer.append(CRLF).append(bodyParam.get(p)).append(CRLF).flush();
				}
			}
			// Send file.
			Map<String, File> fileParam = formParam.getFileParam();
			if (null != fileParam) {
				for (String fileName : fileParam.keySet()) {
					writer.append("--" + boundary).append(CRLF);
					writer.append("Content-Disposition: form-data; name=\"" + fileName + "\"; filename=\""
							+ fileParam.get(fileName).getName() + "\"").append(CRLF);
					writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(fileName)).append(CRLF);
					writer.append("Content-Transfer-Encoding: binary").append(CRLF);
					writer.append(CRLF).flush();
					Files.copy(fileParam.get(fileName).toPath(), output);
					output.flush(); // Important before continuing with writer!
					writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.
				}
			}
			// End of multipart/form-data.
			writer.append("--" + boundary + "--").append(CRLF).flush();
		}
		HttpURLConnection conn = (HttpURLConnection) connection;
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		int len;
		byte[] buffer = new byte[1024];
		while ((len = conn.getInputStream().read(buffer)) != -1) {
			bout.write(buffer, 0, len);
		}
		String result = new String(bout.toByteArray(), "utf-8");
		return result;
	}

FormParam封裝類:

package net.riking.core.utils;

import java.io.File;
import java.util.Map;

public class FormParam {
	private String url;
//	private String auth;
//	/**
//	 * http請求頭里的參數(shù)
//	 */
//	private Map<String, String> headerParam;
	/**
	 * 常規(guī)參數(shù)
	 */
	private Map<String, String> bodyParam;
	/**
	 * 待上傳的文件參數(shù) filename和file
	 */
	private Map<String, File> fileParam;

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

//	public String getAuth() {
//		return auth;
//	}
//
//	public void setAuth(String auth) {
//		this.auth = auth;
//	}
//
//	public Map<String, String> getHeaderParam() {
//		return headerParam;
//	}
//
//	public void setHeaderParam(Map<String, String> headerParam) {
//		this.headerParam = headerParam;
//	}

	public Map<String, String> getBodyParam() {
		return bodyParam;
	}

	public void setBodyParam(Map<String, String> bodyParam) {
		this.bodyParam = bodyParam;
	}

	public Map<String, File> getFileParam() {
		return fileParam;
	}

	public void setFileParam(Map<String, File> fileParam) {
		this.fileParam = fileParam;
	}
}

以上這篇java模擬發(fā)送form-data的請求方式就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • JAVA SpringBoot jar程序 Systemctl生產(chǎn)環(huán)境部署方案

    JAVA SpringBoot jar程序 Systemctl生產(chǎn)環(huán)境部署方案

    這篇文章主要介紹了JAVA SpringBoot jar程序 Systemctl生產(chǎn)環(huán)境部署方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • java中的常用集合類整理

    java中的常用集合類整理

    本篇文章給大家?guī)淼膬?nèi)容是關(guān)于java中List集合及其實(shí)現(xiàn)類的方法介紹(附代碼),有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。下面我們就來學(xué)習(xí)一下吧,希望能給你帶來幫助
    2021-06-06
  • 基于MyBatis的數(shù)據(jù)持久化框架的使用詳解

    基于MyBatis的數(shù)據(jù)持久化框架的使用詳解

    Mybatis是一個優(yōu)秀的開源、輕量級持久層框架,它對JDBC操作數(shù)據(jù)庫的過程進(jìn)行封裝。本文將為大家講解一下基于MyBatis的數(shù)據(jù)持久化框架的使用,感興趣的可以了解一下
    2022-08-08
  • Java中多線程下載圖片并壓縮能提高效率嗎

    Java中多線程下載圖片并壓縮能提高效率嗎

    本文主要介紹了Java中多線程下載圖片并壓縮能提高效率嗎,很多人都想知道這個問題,本文就來詳細(xì)介紹一下,感興趣的小伙伴們可以參考一下
    2021-07-07
  • Java線程中的ThreadLocal類解讀

    Java線程中的ThreadLocal類解讀

    這篇文章主要介紹了Java線程中的ThreadLocal類解讀,ThreadLocal是一個泛型類,作用是實(shí)現(xiàn)線程隔離,ThreadLocal類型的變量,在每個線程中都會對應(yīng)一個具體對象,對象類型需要在聲明ThreadLocal變量時指定,需要的朋友可以參考下
    2023-11-11
  • 淺談Mybatis分頁插件,自定義分頁的坑

    淺談Mybatis分頁插件,自定義分頁的坑

    這篇文章主要介紹了淺談Mybatis分頁插件,自定義分頁的坑,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • Java中有界隊列的飽和策略(reject policy)原理解析

    Java中有界隊列的飽和策略(reject policy)原理解析

    這篇文章主要介紹了Java中有界隊列的飽和策略(reject policy)原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • Java實(shí)現(xiàn)自定義語言和表達(dá)式解析的解釋器模式

    Java實(shí)現(xiàn)自定義語言和表達(dá)式解析的解釋器模式

    Java解釋器設(shè)計模式通過解析自定義語言和表達(dá)式,實(shí)現(xiàn)對復(fù)雜邏輯的處理,提高程序可擴(kuò)展性和靈活性。它將語法解析和執(zhí)行過程分離,通過抽象語法樹和解釋器實(shí)現(xiàn)對語言和表達(dá)式的解析和求值,避免了硬編碼和復(fù)雜的條件判斷,提高了程序的可讀性和可維護(hù)性
    2023-04-04
  • spring與disruptor集成的簡單示例

    spring與disruptor集成的簡單示例

    本篇文章主要介紹了spring與disruptor集成的簡單示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • SpringBoot中使用Zookeeper實(shí)現(xiàn)分布式鎖的案例

    SpringBoot中使用Zookeeper實(shí)現(xiàn)分布式鎖的案例

    本文主要介紹了SpringBoot中使用Zookeeper實(shí)現(xiàn)分布式鎖的案例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-01-01

最新評論

乐昌市| 喜德县| 鄯善县| 中牟县| 保康县| 治多县| 获嘉县| 太谷县| 凤山市| 宁津县| 龙里县| 勃利县| 莱阳市| 获嘉县| 肇州县| 云和县| 常州市| 于都县| 蕉岭县| 克拉玛依市| 绥阳县| 繁峙县| 湘西| 黔东| 双牌县| 湖南省| 丰城市| 同德县| 兴仁县| 洪湖市| 大渡口区| 鲁甸县| 迭部县| 罗甸县| 康马县| 隆安县| 淳化县| 长葛市| 海淀区| 双峰县| 治多县|