Java Server

HTTP的POST调用
import jdk.nashorn.internal.parser.JSONParser;
import net.sf.json.JSONObject;

import java.io.IOException;
import java.net.HttpURLConnection;

import java.net.MalformedURLException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import java.net.URL;
import java.net.URLEncoder;

public class Main {
    public static void main(String[] args) throws IOException {
        for(int i=0; i<30; i++){
            String urlStr = "http://ip地址/reve_post";
            URL url = new URL(urlStr);
            HttpURLConnection con = (HttpURLConnection)url.openConnection();
            //如果要使用URL连接进行输入,请将DoInput标志设置为true,否则将其设置为false
            JSONObject json = new JSONObject();
            json.put("productName", "鞋子");
            json.put("businCatA", "运动户外");
            json.put("businCatB", "运动鞋包");
            json.put("businCatC", "跑步鞋");
            json.put("stock", "1000");
            json.put("fineness", "2");
            json.put("purPrice", "10");
            con.setDoInput(true);
            //如果要使用URL连接进行输出,请将DoInput标志设置为true,否则将其设置为false
            con.setDoOutput (true);
            con.setRequestMethod("POST");

            OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream(),"UTF-8");
            writer.write(json.toString());
            writer.flush();

            BufferedReader reader=new BufferedReader(new InputStreamReader(con.getInputStream()));
//        JSONParser

//        JSONObject
            String response = "";
            String tmp;
            while((tmp=reader.readLine())!=null){
                response += tmp;
            }

            JSONObject obj = JSONObject.fromObject(response);
            System.out.println(response);
            System.out.println("第多少次响应");
            System.out.println(i);
        }
    }
}

参考资料

http://t.zoukankan.com/maosonglin-p-9397257.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值