HttpClient Post请求两种方法。

来源:互联网 发布:编程到底是什么 编辑:程序博客网 时间:2024/05/16 08:01

第一种  不用传参数:

public String getAdminPwd() {String path = "http://10.10.10.20:85/WebSrv/GetPwd";//先通过地址获取流InputStream is = NetUtils.getInputStreamByURI(path);String pwd = null;try {//在通过流来获取json字符串String info = NetUtils.getStringByInputStream(is);//解析jsonJSONObject jo = new JSONObject(info);if (jo != null) {String retcode = jo.getString("retcode");if (retcode.equals("0")) {String retmsg = jo.getString("retmsg");pwd = retmsg;}}} catch (Exception e) {e.printStackTrace();}return pwd;}

// 通过Uri获取流public static InputStream getInputStreamByURI(String path) {// 创建HttpClient对象HttpClient client = new DefaultHttpClient();// 创建HttpPost请求方法HttpPost request = new HttpPost();// 设置URI地址try {request.setURI(new URI(path));} catch (URISyntaxException e) {e.printStackTrace();}HttpResponse response = null;try {response = client.execute(request);} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}InputStream is = null;try {is = response.getEntity().getContent();} catch (IllegalStateException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return is;}

// 通过流获取字符串public static String getStringByInputStream(InputStream is)throws UnsupportedEncodingException {StringBuilder sb = new StringBuilder();BufferedReader br = new BufferedReader(new InputStreamReader(is,"UTF-8"));String temp = null;try {while ((temp = br.readLine()) != null) {sb.append(temp);}} catch (IOException e) {e.printStackTrace();} finally {try {br.close();is.close();} catch (IOException e) {e.printStackTrace();}}return sb.toString();}

第二种   需要传参:

// 登陆/退出上传日志public boolean upLoadLog(String str) {String path = "http://10.10.10.20:85/WebSrv/WriteLog";// InputStream is = NetUtils.getInputStreamByURI(path);// String info = NetUtils.getStringByInputStream(is);boolean isOk = false;//设置参数 可设置多个List<NameValuePair> params = new ArrayList<NameValuePair>();params.add(new BasicNameValuePair("data", str));try {//得到json字符串String info = NetUtils.doPost(params, path);JSONObject jo = new JSONObject(info);if (jo != null) {String retcode = jo.getString("retcode");if (retcode.equals("0")) {isOk = true;}}} catch (Exception e) {e.printStackTrace();}return isOk;}

// 通过Uri获取json字符串public static String doPost(List<NameValuePair> params, String url)throws Exception {String result = null;// 获取HttpClient对象HttpClient httpClient = new DefaultHttpClient();// 新建HttpPost对象HttpPost httpPost = new HttpPost(url);if (params != null) {// 设置字符集HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);// 设置参数实体httpPost.setEntity(entity);}/* * // 连接超时 httpClient.getParams().setParameter( * CoreConnectionPNames.CONNECTION_TIMEOUT, 3000); // 请求超时 * httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, * 3000); */// 获取HttpResponse实例HttpResponse httpResp = httpClient.execute(httpPost);// 获取返回的数据result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");return result;}

以上就是本人所用过的Http post请求方式

0 0
原创粉丝点击