Http的连接方式之HttpUrlConnection

本文通过示例代码详细对比了HTTP GET与POST请求的实现方式,包括URL构造、请求头设置、超时时间设定及响应处理等关键步骤。

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

doget方式

String urlString = "http://localhost:8080/MyWebApp/MyServlet?username=张三&password=123456";
                try {
                    URL url = new URL(urlString);
                    URLConnection connection = url.openConnection();

                    HttpURLConnection httpConnection = (HttpURLConnection)connection;
                    httpConnection.setRequestMethod("GET");

                    // 设置接受的数据类型
                    httpConnection.setRequestProperty("Accept-Charset", "utf-8");
                    // 设置可以接受序列化的java对象
                    httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    //设置连接超时的时间
                    httpConnection.setConnectTimeout(3000);
                    //设置读取超时
                    httpConnection.setReadTimeout(3000);
                    int code =httpConnection.getResponseCode();
                    System.out.println("http状态码:"+code);
                    if(code==HttpURLConnection.HTTP_OK){
                        InputStream is= httpConnection.getInputStream();
                        BufferedReader br = new BufferedReader(new InputStreamReader(is));
                        String line = br.readLine();
                        while(line!=null){
                            System.out.println(line);
                            line=br.readLine();
                        }
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch(SocketTimeoutException e){
                    System.out.println("网络连接超时");
                } catch (IOException e) {
                    e.printStackTrace();
                }

dopost方式

String urlString = "http://localhost:8080/WebServer/MyServletJSON";
                try {
                    URL url = new URL(urlString);
                    HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();

                    /**
                     * post和get的不同点
                     */
                    //设置提交方法为post
                    httpConnection.setRequestMethod("POST");
                    //设置可以可以获取服务器返回的内容,默认为true
//                  httpConnection.setDoInput(true);
                    //设置客户端可以给服务器提交数据,默认是false的。post方法必须为true
                    httpConnection.setDoOutput(true);
                    //post方法不允许使用缓存
                    httpConnection.setUseCaches(false);
                    /**
                     * 
                     */



                    // 设置接受的数据类型
                    httpConnection.setRequestProperty("Accept-Charset", "utf-8");
                    // 设置可以接受序列化的java对象
                    httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    // 设置连接超时的时间
//                  httpConnection.setConnectTimeout(3000);
//                  // 设置读取超时
//                  httpConnection.setReadTimeout(3000);

                    /**
                     * 设置向服务器提交的参数
                     */

                    String params = "jsoncontent=zhangsan";
                    httpConnection.getOutputStream().write(params.getBytes());


                    int code = httpConnection.getResponseCode();
                    System.out.println(code);
                    if(code==HttpURLConnection.HTTP_OK){
                        InputStream is = httpConnection.getInputStream();
                        BufferedReader br = new BufferedReader(new InputStreamReader(is));
                        String line = br.readLine();
                        while(line!=null){
                            System.out.println(line);
                            line = br.readLine();
                        }
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值