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

本文详细介绍了如何使用HttpURLConnection类向服务器发送GET和POST请求的方法,包括发送请求、读取服务器响应数据的过程,并展示了参数的URL编码和设置请求头的方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

转自:http://www.cnblogs.com/linjiqin/archive/2011/09/19/2181634.html

一、使用HttpURLConnection向服务器发送get请求

1、向服务器发送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");
        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");

《Java后台利用HttpURLConnection模拟浏览器请求实战》一书为Java开发者提供了在后台使用HttpURLConnection发送HTTP请求的实战技巧。在进行GET请求时,通过建立与目标URL的连接,并读取响应数据,能够有效地实现数据的获取。具体步骤包括创建URL对象、建立连接、获取输入流以及读取数据。示例代码如下: 参考资源链接:[Java后台利用HttpURLConnection模拟浏览器请求实战](https://wenku.youkuaiyun.com/doc/645ba92695996c03ac2d8849?spm=1055.2569.3001.10343) (代码示例略) 而在实现POST请求时,关键在于设置请求方法为POST,发送请求数据,并处理服务器响应。在这个过程中,需要设置HttpURLConnection的输出流,并正确编码写入POST参数。示例代码如下: (代码示例略) 通过这两个示例代码,我们可以看到如何在Java后台通过HttpURLConnection类发送GETPOST请求。为了进一步提高代码的健壮性,建议添加异常处理错误检查机制。此外,对于涉及敏感数据的操作,还应该考虑使用HTTPS协议来确保数据传输的安全性。 在掌握基础的HTTP请求发送之后,为了更好地理解HttpURLConnection在实际开发中的应用,推荐深入阅读《Java后台利用HttpURLConnection模拟浏览器请求实战》。该书不仅提供了实用的实例,还涵盖了相关的知识点高级用法,帮助开发者在实际项目中更有效地运用HttpURLConnection进行网络请求。 参考资源链接:[Java后台利用HttpURLConnection模拟浏览器请求实战](https://wenku.youkuaiyun.com/doc/645ba92695996c03ac2d8849?spm=1055.2569.3001.10343)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值