使用HttpURLConnection向服务器发送post和get请求

//向服务器发送get请求

@Test
    publicvoid sendSms() throws Exception{
        String message="货已发到";
        message=URLEncoder.encode(message, "UTF-8");
        System.out.println(message);
        String path ="http://localhost:8083/DS_Trade/mobile/sim!add.do?message="+message;
        URL url =new URL(path);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setConnectTimeout(5*1000);
        conn.setRequestMethod("GET");

        conn.connect();
        InputStream inStream = conn.getInputStream();   
        byte[] data = StreamTool.readInputStream(inStream);
        String result=new String(data, "UTF-8");
        System.out.println(result);
    }

 

2、从服务器读取数据    

String message=request.getParameter("message");

 

 

二、使用HttpURLConnection向服务器发送post请求

1、向服务器发送post请求

@Test
    publicvoid addByUrl() throws Exception{
        String encoding="UTF-8";
        String params="[{\"addTime\":\"2011-09-19 14:23:02\"[],\"iccid\":\"1111\",\"id\":0,\"imei\":\"2222\",\"imsi\":\"3333\",\"phoneType\":\"4444\",\"remark\":\"aaaa\",\"tel\":\"5555\"}]";
        String path ="http://localhost:8083/xxxx/xxx/sim!add.do";
        byte[] data = params.getBytes(encoding);
        URL url =new URL(path);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        //application/x-javascript text/xml->xml数据 application/x-javascript->json对象 application/x-www-form-urlencoded->表单数据
        conn.setRequestProperty("Content-Type", "application/x-javascript; charset="+ encoding);
        conn.setRequestProperty("Content-Length", String.valueOf(data.length));
        conn.setConnectTimeout(5*1000);
        OutputStream outStream = conn.getOutputStream();
        outStream.write(data);
        outStream.flush();
        outStream.close();
        System.out.println(conn.getResponseCode()); //响应代码 200表示成功
        if(conn.getResponseCode()==200){
            InputStream inStream = conn.getInputStream();  
            String result=new String(StreamTool.readInputStream(inStream), "UTF-8");
        }
    }

 

2、从服务器读取数据   

//获取post请求过来的数据
byte[] data=StreamTool.readInputStream(request.getInputStream());
        //[{\"addTime\":\"2011-09-19 14:23:02\"[],\"iccid\":\"1111\",\"id\":0,\"imei\":\"2222\",\"imsi\":\"3333\",\"phoneType\":\"4444\",\"remark\":\"aaaa\",\"tel\":\"5555\"}]  
      
String json=new String(data, "UTF-8");

 

 3、服务器端的响应处理

 

response.setContentType("application/octet-stream");//浏览器无法使用某种方式或激活某个程序来处理的MIME类型
   
   ServletOutputStream os = response.getOutputStream();

 

String rsp = "User_name=002&errcode=0&errmsg=测试";
   
   os.write(rsp.getBytes("UTF-8"));

 

//下面的方式为处理 对象类型 的响应数据。

//BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(os);
//   ObjectOutputStream objectOutputStream = new ObjectOutputStream(bufferedOutputStream);
//   
//   objectOutputStream.writeObject(rsp);
//   objectOutputStream.writeBytes(rsp);
   os.flush();
//   objectOutputStream.close();

 

 

 

ps:客户端对对象类型的解析方法

InputStream is = postMethod.getResponseBodyAsStream();

   /*
    * BufferedReader br = new BufferedReader(new
    * InputStreamReader(is));
    *
    * String objString = ""; String tempString = ""; while ((tempString =
    * br.readLine()) != null) { objString += tempString; }
    */
   ObjectInputStream objectInputStream = new ObjectInputStream(new BufferedInputStream(is));

   response = (Response) objectInputStream.readObject();
   objectInputStream.close();

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值