您的位置:首页 > 理论基础 > 计算机网络

Http的连接方式之HttpUrlConnection

2015-08-13 22:03 645 查看

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();
                }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: