首页 技术 正文
技术 2022年11月20日
0 收藏 429 点赞 2,869 浏览 5586 个字

/**
         * 用 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();
        }

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,486
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,126
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289