URLConnection的GET方法与POST方法,HttpClient的GET方法与POST方法

URLConnection的GET方法与POST方法

GET方法

代码如下:

String urlstring = "http://localhost:8080/MyserverTest/Test?name=zhaoliu&password=12";
                try {
                    URL url = new URL(urlstring);
                    URLConnection connect = url.openConnection();
                    HttpURLConnection httpconnection =(HttpURLConnection)connect;
                    //设置接收服务器的类型为GET
                    httpconnection.setConnectTimeout(30000);
                    httpconnection.setReadTimeout(30000);
                    httpconnection.setRequestMethod("GET");//用GET方法去接收服务器发来的数据。 
                    //设置接收的数据类型
                    httpconnection.setRequestProperty("Accept-Charset", "utf-8");
                    // 设置可以接受序列化的java对象
                    httpconnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    //获得http状态码若为200说明接通正常

                    int code =httpconnection.getResponseCode();
                    if(code==HttpsURLConnection.HTTP_OK){
                        //建立接受数据流

                        BufferedReader br=new BufferedReader(new InputStreamReader(httpconnection.getInputStream()));

                        String line =br.readLine();
                        while(line!=null){
                            System.out.println(line);
                            line =br.readLine();
                        }
                    }

                } catch(SocketTimeoutException e){
                    System.out.println("获取数据超时");

                } catch(ConnectException e){
                    System.out.println("网络连接超时");

                }catch(MalformedURLException e) {

                    e.printStackTrace();
                } catch (IOException e) {

                    e.printStackTrace();
                }

POST方法

String urlstring = "http://localhost:8080/MyserverTest/Test";
                try {
                    URL url = new URL(urlstring);
                    URLConnection connect = url.openConnection();

                    HttpURLConnection httpconnection =(HttpURLConnection)connect;
                    //设置接收服务器的类型为GET
                    httpconnection.setConnectTimeout(30000);
                    httpconnection.setReadTimeout(30000);
                    //设置请求的方法是POST,默认是GET。
                    httpconnection.setRequestMethod("POST");                                        
                    //设置接收的数据类型
                    httpconnection.setRequestProperty("Accept-Charset", "utf-8");
                    // 设置可以接受序列化的java对象
                    httpconnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    //获得http状态码若为200说明接通正常
                    httpconnection.setDoOutput(true);//设置向服务器发送数据是允许的,在post方法中这是必须为true
                    httpconnection.setDoInput(true);//设置可读取服务器的返回值,默认为true,可以不用设置
                    httpconnection.setUseCaches(false);//post方法不允许使用缓存
                    String s = "name=zhansan&password=123";
                    httpconnection.getOutputStream().write(s.getBytes());//设置输出流
                    int code =httpconnection.getResponseCode();//获得http状态码
                    if(code==HttpsURLConnection.HTTP_OK){//如果连接成功
                        //建立接受数据流
                        BufferedReader br=new BufferedReader(new InputStreamReader(httpconnection.getInputStream()));

                        String line =br.readLine();
                        while(line!=null){
                            System.out.println(line);
                            line =br.readLine();
                        }
                    }

                } catch(SocketTimeoutException e){
                    System.out.println("获取数据超时");

                } catch(ConnectException e){
                    System.out.println("网络连接超时");

                }catch(MalformedURLException e) {

                    e.printStackTrace();
                } catch (IOException e) {

                    e.printStackTrace();
                }

HttpClient的GET方法与POST方法

GET

   HttpClient client = new DefaultHttpClient();
            String URLString = "http://192.168.0.43:8080/www/MyserverTest?name=zhaoliu&password=1234";

            HttpGet get=new HttpGet(URLString);//设置为get方法
            get.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            //设置服务器接收后数据的读取方式为utf8
            try {
                HttpResponse response=client.execute(get);//执行get方法得到服务器的返回的所有数据都在response中
                StatusLine statusLine=response.getStatusLine();//httpClient访问服务器返回的表头,包含http状态码
                int code=statusLine.getStatusCode();//得到状态码
                if(code== HttpURLConnection.HTTP_OK){
                    HttpEntity entity=response.getEntity();//得到数据的实体
                    InputStream is=entity.getContent();//得到输入流
                    BufferedReader br=new BufferedReader(new InputStreamReader(is));
                    String line=br.readLine();
                    while(line!=null){
                       Log.d("aaaaaaaaaa",line);
                        line=br.readLine();
                    }
                }
            } catch (ClientProtocolException e1) {

                e1.printStackTrace();
            } catch (IOException e1) {

                e1.printStackTrace();
            }

POST方法

 HttpClient client = new DefaultHttpClient();
            String URLString = "http://192.168.0.43:8080/www/MyserverTest?name=zhaoliu&password=12";
            HttpPost post =new HttpPost(URLString);
            NameValuePair pair1 =new BasicNameValuePair("name", "zhaoliu");
            NameValuePair pair2 =new BasicNameValuePair("password", "12");
            ArrayList<NameValuePair> array = new ArrayList<>();
            array.add(pair1);
            array.add(pair2);
            try {
                post.setEntity(new UrlEncodedFormEntity(array, "UTF-8"));
                post.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
                HttpResponse request = client.execute(post);
                int code= request.getStatusLine().getStatusCode();
                if(code==200){
                    HttpEntity entity=request.getEntity();
                    InputStream is =entity.getContent();
                    BufferedReader br =new BufferedReader(new InputStreamReader(is));
                    String line =br.readLine();
                    while(line!=null){
                        System.out.println(line);
                        line =br.readLine();
                    }

                }
            } catch (UnsupportedEncodingException e1) {

                e1.printStackTrace();
            } catch (IOException e1) {

                e1.printStackTrace();
            }

你可能感兴趣的:(URLConnection的GET方法与POST方法,HttpClient的GET方法与POST方法)