httpPost的form-data请求方式

package com.choice.test;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.io.IOUtils;


public class HttpPostTest {
    public static void main(String[] args) throws Exception {
        String result = createDocket();
        System.out.println(result);
    }
    
    public static String createDocket() {
        String API_URL = "https://uat.logisticsgrid.in/webservice/v1/CreateDocket";
    
        String USER_AGENT = "Mozilla/5.0";
        String POST_PARAMS = "SecureKey=3EDAE9A62C9649D38E3FBFF29ACECB1A&FromOU=&DocketNumber=3334&"
                + "DeliveryDate=2019-01-17&CustomerCode=TEST001&ConsigneeCode=&ConsigneeAddress=CESHI&"
                + "ConsigneeCountry=KE&ConsigneeState=NAIROBI&ConsigneeCity=Nairobi&ConsigneePincode=&"
                + "ConsigneeName=Miss&ConsigneePhone=15521244709&StartLocation=&EndLocation="
                + "&ClientCode=&NumberOfPackages=1&ActualWeight=123&ChargedWeight=&CargoValue="
                + "&ReferenceNumber=CESHI20191511&InvoiceNumber=&PaymentMode=TBB"
                + "&ServiceCode=LPSVR&WeightUnitType=&ConsingmentType=&Description=&COD=&CODPaymentMode=&PackageDetails=";
        String aString = API_URL + "?" + POST_PARAMS;
        URL obj;
        HttpURLConnection con;
        BufferedReader in;
        StringBuffer response = new StringBuffer();
        String responseData;
    
        try { 
          obj = new URL(API_URL);
          con = (HttpURLConnection) obj.openConnection();
          con.setRequestMethod("POST");
          con.setRequestProperty("User-Agent", USER_AGENT);
          con.setDoOutput(true);
          OutputStream os = con.getOutputStream();
          os.write(POST_PARAMS.getBytes());
          os.flush();
          os.close();
          
          in = new BufferedReader(new InputStreamReader(con.getInputStream()));
          while ((responseData = in.readLine()) != null) {
              response.append(responseData);
          }
          in.close();
        } catch(IOException io) {
          responseData = io.getMessage();
        }
        responseData = response.toString();
        return responseData;
    }

}

你可能感兴趣的:(Java,URL跨域请求代码)