java 模拟表单post提交

 Java

/**
 * 模拟提交表单
 * @param url
 * @param sb
 */
public static String postByFormData(String url,StringBuffer sb){
    String responseMessage = "";
    StringBuffer response = new StringBuffer();
    HttpURLConnection httpConnection = null;
    OutputStreamWriter out = null;
    BufferedReader reader = null;
    try {
        URL urlPost = new URL(url);
        httpConnection = (HttpURLConnection) urlPost.openConnection();
        httpConnection.setDoOutput(true);
        httpConnection.setDoInput(true);
        // 参数长度太大,不能用get方式
        httpConnection.setRequestMethod("POST");
        // 不使用缓存
        httpConnection.setUseCaches(false);
        // URLConnection.setInstanceFollowRedirects是成员函数,仅作用于当前函数
        httpConnection.setInstanceFollowRedirects(true);
        // 配置本次连接的Content-type,配置为application/x-www-form-urlencoded的
        // 意思是正文是urlencoded编码过的form参数
        httpConnection.setRequestProperty("Connection", "Keep-Alive");
        // 设置请求头信息
        httpConnection.setRequestProperty("Charset", "UTF-8");
        // 设置边界
        String BOUNDARY = "----------" + System.currentTimeMillis();
        httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        httpConnection.connect();
        out = new OutputStreamWriter(httpConnection.getOutputStream(),"UTF-8");
        //写入参数,DataOutputStream.writeBytes将字符串中的16位的unicode字符以8位的字符形式写道流里面
        out.write(sb.toString());
        System.out.println("send_url:" + url);
        System.out.println("send_data:" + sb.toString());
        // flush and close
        out.flush();

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (null != out) {
                out.close();
            }
            if (null != reader) {
                reader.close();
            }
            if (null != httpConnection) {
                httpConnection.disconnect();
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }

    try {
        reader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream(),"UTF-8"));
        while ((responseMessage = reader.readLine()) != null) {
            response.append(responseMessage);
            response.append("\n");
        }
        if (!"failure".equals(response.toString())) {
            System.out.println("success");
        } else {
            System.out.println("failue");
        }
        // 将该url的配置信息缓存起来
        System.out.println(response.toString());
        System.out.println(httpConnection.getResponseCode());
    } catch (IOException e) {
        e.printStackTrace();
    }

    return response.toString();
}

 

 

测试

public static void main(args[]){

 

String url="http://test.cn/default/index/calculate-shipping-freight";
StringBuffer sb=new StringBuffer();
sb.append("toCountry="+ 2);//国家要传2字码
sb.append("&toCity="+ "BERAT");
sb.append("&logistics-check-express="+"UPS-GY");
sb.append("&logistics-express-item[]="+"BH-HKDHL");
sb.append("&packageWeight="+1);
sb.append("&packageLength="+2);
sb.append("&packageWidth="+3);
sb.append("&packageHeight="+4);
try {
    String post = postByFormData(url,sb);
    jsonResult.setValue(post);
}catch ( Exception e){
    e.printStackTrace();
}

 

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值