91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java如何模擬發送form-data的請求

發布時間:2020-07-29 09:14:38 來源:億速云 閱讀:655 作者:小豬 欄目:編程語言

這篇文章主要講解了java如何模擬發送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("發送消息收到的返回:" + buffer.toString());
  }
}

補充知識:java模擬復雜表單post請求

java模擬復雜表單post請求

能支持文件上傳

/**
	 * 支持復雜表單,比如文件上傳
	 * @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請求頭里的參數
//	 */
//	private Map<String, String> headerParam;
	/**
	 * 常規參數
	 */
	private Map<String, String> bodyParam;
	/**
	 * 待上傳的文件參數 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如何模擬發送form-data的請求有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

青铜峡市| 鸡东县| 雷山县| 大田县| 府谷县| 天全县| 浦东新区| 鸡东县| 崇信县| 犍为县| 邹平县| 高邮市| 同仁县| 湖口县| 马尔康县| 松江区| 商河县| 阳东县| 城步| 张家口市| 陆河县| 蓬安县| 越西县| 崇左市| 加查县| 广汉市| 个旧市| 贵港市| 襄城县| 句容市| 木里| 沾益县| 沙洋县| 万载县| 通榆县| 密山市| 三穗县| 门头沟区| 巫山县| 辉南县| 全南县|