Apache提供HttpPost

package com.bgxt.httpUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public class httpClientUtils implements Runnable {
    /**
     * 对于Android4.0之上的环境下,不能在主线程中访问网络 所以这里另新建了一个实现了Runnable接口的Http访问类
     */
    private String username;
    private String password;

    public httpClientUtils(String username, String password) {
        // 初始化用户名和密码
        this.username = username;
        this.password = password;
    }

    @Override
    public void run() {
        // 设置访问的Web站点
        String path = "http://192.168.1.103:1231/loginas.ashx";
        // 设置Http请求参数
        Map<String, String> params = new HashMap<String, String>();
        params.put("username", username);
        params.put("password", password);

        String result = sendHttpClientPost(path, params, "utf-8");
        // 把返回的接口输出
        System.out.println(result);
    }

    /**
     * 发送Http请求到Web站点
     * 
     * @param path
     *            Web站点请求地址
     * @param map
     *            Http请求参数
     * @param encode
     *            编码格式
     * @return Web站点响应的字符串
     */
    private String sendHttpClientPost(String path, Map<String, String> map,
            String encode) {
        List<NameValuePair> list = new ArrayList<NameValuePair>();
        if (map != null && !map.isEmpty()) {
            for (Map.Entry<String, String> entry : map.entrySet()) {
                // 解析Map传递的参数,使用一个键值对对象BasicNameValuePair保存。
                list.add(new BasicNameValuePair(entry.getKey(), entry
                        .getValue()));
            }
        }
        try {
            // 实现将请求 的参数封装封装到HttpEntity中。
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, encode);
            // 使用HttpPost请求方式
            HttpPost httpPost = new HttpPost(path);
            // 设置请求参数到Form中。
            httpPost.setEntity(entity);
            // 实例化一个默认的Http客户端
            DefaultHttpClient client = new DefaultHttpClient();
            // 执行请求,并获得响应数据
            HttpResponse httpResponse = client.execute(httpPost);
            // 判断是否请求成功,为200时表示成功,其他均问有问题。
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                // 通过HttpEntity获得响应流
                InputStream inputStream = httpResponse.getEntity().getContent();
                return changeInputStream(inputStream, encode);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 把Web站点返回的响应流转换为字符串格式
     * 
     * @param inputStream
     *            响应流
     * @param encode
     *            编码格式
     * @return 转换后的字符串
     */
    private String changeInputStream(InputStream inputStream, String encode) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] data = new byte[1024];
        int len = 0;
        String result = "";
        if (inputStream != null) {
            try {
                while ((len = inputStream.read(data)) != -1) {
                    outputStream.write(data, 0, len);
                }
                result = new String(outputStream.toByteArray(), encode);

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

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值