zoukankan      html  css  js  c++  java
  • http 网络请求

    /**
             * 用 HttpClient 的 Get 请求访问服务器
             *
             * @param url_path
             * @param userName
             * @param userPass
             * @return
             */
            private String HttpClientGetMeth(String url_path, String userName,
                    String userPass) {
                String result = "";

                try {
                    url_path = url_path + "?username="
                            + URLEncoder.encode(userName, "utf-8") + "&userpass="
                            + URLEncoder.encode(userPass, "utf-8");

                    HttpGet get = new HttpGet(url_path);
                    HttpParams params = new BasicHttpParams();
                    HttpConnectionParams.setConnectionTimeout(params, 5 * 1000);
                    HttpConnectionParams.setSoTimeout(params, 5 * 1000);

                    HttpClient httpClient = new DefaultHttpClient(params);
                    HttpResponse response = httpClient.execute(get);

                    if (response.getStatusLine().getStatusCode() == 200) {
                        HttpEntity resEntity = response.getEntity();
                        result = EntityUtils.toString(resEntity, "utf-8");
                    }
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return result;
            }

            /**
             * 用 HttpClient 的 Post 请求访问服务器
             *
             * @param url_path
             * @param userName
             * @param userPass
             * @return
             */
            private String HttpClientPostMeth(String url_path, String userName,
                    String userPass) {
                String result = "";

                HttpPost post = new HttpPost(url_path);
                HttpParams params = new BasicHttpParams();
                HttpConnectionParams.setConnectionTimeout(params, 5 * 1000);
                HttpConnectionParams.setSoTimeout(params, 5 * 1000);

                List<NameValuePair> pair = new ArrayList<NameValuePair>();
                pair.add(new BasicNameValuePair("username", userName));
                pair.add(new BasicNameValuePair("userpass", userPass));

                try {
                    HttpEntity entity = new UrlEncodedFormEntity(pair, "utf-8");
                    post.setEntity(entity);

                    HttpClient httpClient = new DefaultHttpClient(params);
                    HttpResponse response = httpClient.execute(post);

                    if (response.getStatusLine().getStatusCode() == 200) {
                        HttpEntity resEntity = response.getEntity();
                        result = EntityUtils.toString(resEntity, "utf-8");
                    }

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

                return result;
            }

            /**
             * 用 HttpURLConnection 的 Post 请求访问服务器
             *
             * @param url_path
             * @param userName
             * @param userPass
             * @return
             */
            private String URLConnectionPosttMeth(String url_path, String userName,
                    String userPass) {
                String result = "";
                // ?username=admin&userpass=123456
                try {
                    URL url = new URL(url_path);
                    HttpURLConnection conn = (HttpURLConnection) url
                            .openConnection();
                    conn.setConnectTimeout(5 * 1000);
                    conn.setReadTimeout(5 * 1000);

                    conn.setRequestMethod("POST");// 设置请求方式为 Post 方式
                    conn.setDoInput(true);// 设置是否可以读取
                    conn.setDoOutput(true);// 设置是否可以写入

                    DataOutputStream dos = new DataOutputStream(
                            conn.getOutputStream());
                    // 把中文进行utf-8编码,服务器通过request.setCharacterEncoding("utf-8");解码
                    String params = "username="
                            + URLEncoder.encode(userName, "utf-8") + "&userpass="
                            + URLEncoder.encode(userPass, "utf-8");

                    dos.write(params.getBytes());
                    dos.flush();
                    dos.close();

                    if (conn.getResponseCode() == 200) {
                        InputStream is = conn.getInputStream();// 获得输入流对象读取服务器响应结果

                        // 服务器需要通过response.setCharacterEncoding("utf-8");//设置服务器响应编码为中文编码,为了解决android端接收的数据能不乱码
                        // 因为有中文乱码,需要转码,通过把字节流转换为缓存字符流,同时设置编码,实现转码
                        InputStreamReader reader = new InputStreamReader(is,
                                "utf-8");

                        char[] buf = new char[1024];
                        reader.read(buf);
                        // Log.i("Bright", buf.length + "------post------");
                        result = new String(buf, 0, buf.length);
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (ProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return result;
            }

            /**
             * 用 HttpURLConnection 的 Get 请求访问服务器
             *
             * @param url_path
             * @param userName
             * @param userPass
             * @return
             */
            private String URLConnectionGetMeth(String url_path, String userName,
                    String userPass) {
                String result = "";
                // ?username=admin&userpass=123456
                try {
                    // 把中文进行utf-8编码,服务器通过request.setCharacterEncoding("utf-8");解码
                    url_path = url_path + "?username="
                            + URLEncoder.encode(userName, "utf-8") + "&userpass="
                            + URLEncoder.encode(userPass, "utf-8");

                    URL url = new URL(url_path);
                    HttpURLConnection conn = (HttpURLConnection) url
                            .openConnection();
                    conn.setConnectTimeout(5 * 1000);
                    conn.setReadTimeout(5 * 1000);
                    if (conn.getResponseCode() == 200) {
                        InputStream is = conn.getInputStream();

                        // 服务器需要通过response.setCharacterEncoding("utf-8");//设置服务器编码为中文编码,为了解决android端接收的数据能不乱码
                        // 因为有中文乱码,需要转码,通过把字节流转换为缓存字符流,同时设置编码,实现转码
                        InputStreamReader reader = new InputStreamReader(is,
                                "utf-8");

                        char[] buf = new char[1024];
                        reader.read(buf);
                        // Log.i("Bright", buf.length + "------get------");
                        result = new String(buf, 0, buf.length);
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return result.trim();
            }

  • 相关阅读:
    [PKUWC2018][LOJ2537]Minimax(线段树合并)
    [NOI2019][洛谷P5471]弹跳(dijkstra+KD-Tree)
    [BZOJ4770]图样(概率期望、二进制数位dp)
    [SPOJ11482][BZOJ2787]Count on a trie(广义SA+长链剖分+BIT)
    [HEOI/TJOI2016][洛谷P4094]字符串(SA+主席树)
    [BZOJ3270]博物馆(矩阵求逆)
    [NOI2016][洛谷P1117]优秀的拆分(SA)
    [NOI2018][洛谷P4770]你的名字(SAM+SA+主席树)
    设置echarts两个y轴的0点一致
    echarts中饼图或环形图的高亮效果(点击高亮/默认某一条高亮)
  • 原文地址:https://www.cnblogs.com/BrightPoplar/p/4835659.html
Copyright © 2011-2022 走看看