MultipartRequestEntity post 请求发送。接收端用java httpserver httpexchange 接受。能不能单独取出 request sign 两块内容

package com.test.HttpClient;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StreamUtils;
/**

  • Hello world!
    *
    */
    public class HttpClientApp
    {
    private static final Logger logger = LoggerFactory.getLogger(HttpClientApp.class);
    public static void main( String[] args )
    {
    String url="http://127.0.0.1:8081/SpringMvc1/student";
    String reqStr="aaaaaaa";
    String contentType="application/json";
    String charset="UTF-8";
    String ss=doPost( url, reqStr, contentType, charset) ;
    System.out.println("返回内容为1:" +ss);
    Part[] parts=new Part[2];
    parts[0]=new StringPart("request", "请求报文体",charset);
    parts[1]=new StringPart("sign", "数字签名",charset);
    url="http://127.0.0.1:8081/SpringMvc1/BossServlet";
    // url="http://127.0.0.1:8081/SpringMvc1/fileupload";

    String response=doPostMuStr( url,  parts) ;
    System.out.println("返回内容为1:" +response);
    

    }
    /**

    • 执行一个HTTP POST请求,返回请求响应的HTML *
    • @param url 请求的URL地址
    • @param reqStr 请求的查询参数,可以为null
    • @param charset 字符集
    • @return 返回请求响应的HTML
      */
      public static String doPost(String url, String reqStr, String contentType, String charset) {
      HttpClient client = new HttpClient();

      PostMethod method = new PostMethod(url);
      try {
      HttpConnectionManagerParams managerParams = client.getHttpConnectionManager().getParams();
      managerParams.setConnectionTimeout(30000); // 设置连接超时时间(单位毫秒)
      managerParams.setSoTimeout(30000); // 设置读数据超时时间(单位毫秒)

      method.setRequestEntity(new StringRequestEntity(reqStr, contentType, "utf-8"));
      
      client.executeMethod(method);
      System.out.println("返回的状态码为:" +method.getStatusCode());
      if (method.getStatusCode() == HttpStatus.SC_OK) {
      

      // return StreamUtils.copyToString(method.getResponseBodyAsStream(), Charset.forName(charset));
      return IOUtils.toString(method.getResponseBodyAsStream(),"utf-8");

      }
      

      } catch (UnsupportedEncodingException e1) {
      logger.error(e1.getMessage());
      return "";
      } catch (IOException e) {
      logger.error("执行HTTP Post请求" + url + "时,发生异常!" + e.toString());
      return "";
      } finally {
      method.releaseConnection();
      }
      return null;
      }
      /**

    • 执行一个HTTP POST请求,返回请求响应的HTML
      *

    • @param url 请求的URL地址

    • @param reqStr 请求的查询参数,可以为null

    • @param charset 字符集

    • @return 返回请求响应的HTML
      */
      public static String doPostMuStr(String url, Part[] reqStr) {
      HttpClient client = new HttpClient();

      PostMethod method = new PostMethod(url);
      try {
      HttpConnectionManagerParams managerParams = client.getHttpConnectionManager().getParams();
      managerParams.setConnectionTimeout(30000); // 设置连接超时时间(单位毫秒)
      managerParams.setSoTimeout(30000); // 设置读数据超时时间(单位毫秒)

      method.setRequestEntity(new MultipartRequestEntity(reqStr, method.getParams()));
      

      // method.setContentChunked(true);
      client.executeMethod(method);
      System.out.println("返回的状态码为:" +method.getStatusCode());
      if (method.getStatusCode() == HttpStatus.SC_OK) {
      return IOUtils.toString(method.getResponseBodyAsStream(),"utf-8");

      }
      } catch (UnsupportedEncodingException e1) {
      logger.error(e1.getMessage());
      return "";
      } catch (IOException e) {
      logger.error("执行HTTP Post请求" + url + "时,发生异常!" + e.toString());
      return "";
      } finally {
      method.releaseConnection();
      }
      return null;
      }

      }

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问