HttpClient4使用

HttpClient4使用packagemain;importjava.io.IOException;importjava.io.UnsupportedEncodingException;importjava.net

大家好,又见面了,我是你们的朋友全栈君。

 

package main;
 
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
 
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
 
public class Main {
 
    private static HttpClient hc = new DefaultHttpClient();
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("email", "xxx@gmail.com"));
        params.add(new BasicNameValuePair("pwd", "xxx"));
        params.add(new BasicNameValuePair("save_login", "1"));
 
        String url = "http://www.oschina.net/action/user/login";
 
        String body = post(url, params);
        System.out.println(body);
    }
 
    /**
     * Get请求
     * @param url
     * @param params
     * @return
     */
    public static String get(String url, List<NameValuePair> params) {
        String body = null;
        try {
            // Get请求
            HttpGet httpget = new HttpGet(url);
            // 设置参数
            String str = EntityUtils.toString(new UrlEncodedFormEntity(params));
            httpget.setURI(new URI(httpget.getURI().toString() + "?" + str));
            // 发送请求
            HttpResponse httpresponse = hc.execute(httpget);
            // 获取返回数据
            HttpEntity entity = httpresponse.getEntity();
            body = EntityUtils.toString(entity);
            if (entity != null) {
                entity.consumeContent();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return body;
    }
 
    /**
     * // Post请求
     * @param url
     * @param params
     * @return
     */
    public static String post(String url, List<NameValuePair> params) {
        String body = null;
        try {
            // Post请求
            HttpPost httppost = new HttpPost(url);
            // 设置参数
            httppost.setEntity(new UrlEncodedFormEntity(params));
            // 发送请求
            HttpResponse httpresponse = hc.execute(httppost);
            // 获取返回数据
            HttpEntity entity = httpresponse.getEntity();
            body = EntityUtils.toString(entity);
            if (entity != null) {
                entity.consumeContent();
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return body;
    }
 
}

  

2. HttpClient4使用     

package 
main;
 
import 
java.io.IOException;
import 
java.io.UnsupportedEncodingException;
import 
java.net.URI;
import 
java.net.URISyntaxException;
import 
java.util.ArrayList;
import 
java.util.List;
 
import 
org.apache.http.HttpEntity;
import 
org.apache.http.HttpResponse;
import 
org.apache.http.NameValuePair;
import 
org.apache.http.ParseException;
import 
org.apache.http.client.ClientProtocolException;
import 
org.apache.http.client.HttpClient;
import 
org.apache.http.client.entity.UrlEncodedFormEntity;
import 
org.apache.http.client.methods.HttpGet;
import 
org.apache.http.client.methods.HttpPost;
import 
org.apache.http.impl.client.DefaultHttpClient;
import 
org.apache.http.message.BasicNameValuePair;
import 
org.apache.http.util.EntityUtils;
 
public 
class 
Main {
 
    
private 
static 
HttpClient hc = 
new 
DefaultHttpClient();
 
    
/**
     
* @param args
     
*/
    
public 
static 
void 
main(String[] args) {
        
List<NameValuePair> params = 
new 
ArrayList<NameValuePair>();
        
params.add(
new 
BasicNameValuePair(
"email"

"xxx@gmail.com"
));
        
params.add(
new 
BasicNameValuePair(
"pwd"

"xxx"
));
        
params.add(
new 
BasicNameValuePair(
"save_login"

"1"
));
 
        
String url = 
"http://www.oschina.net/action/user/login"
;
 
        
String body = post(url, params);
        
System.out.println(body);
    
}
 
    
/**
     
* Get请求
     
* @param url
     
* @param params
     
* @return
     
*/
    
public 
static 
String get(String url, List<NameValuePair> params) {
        
String body = 
null
;
        
try 
{
            
// Get请求
            
HttpGet httpget = 
new 
HttpGet(url);
            
// 设置参数
            
String str = EntityUtils.toString(
new 
UrlEncodedFormEntity(params));
            
httpget.setURI(
new 
URI(httpget.getURI().toString() + 
"?" 
+ str));
            
// 发送请求
            
HttpResponse httpresponse = hc.execute(httpget);
            
// 获取返回数据
            
HttpEntity entity = httpresponse.getEntity();
            
body = EntityUtils.toString(entity);
            
if 
(entity != 
null
) {
                
entity.consumeContent();
            
}
        

catch 
(ParseException e) {
            
e.printStackTrace();
        

catch 
(UnsupportedEncodingException e) {
            
e.printStackTrace();
        

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

catch 
(URISyntaxException e) {
            
e.printStackTrace();
        
}
        
return 
body;
    
}
 
    
/**
     
* // Post请求
     
* @param url
     
* @param params
     
* @return
     
*/
    
public 
static 
String post(String url, List<NameValuePair> params) {
        
String body = 
null
;
        
try 
{
            
// Post请求
            
HttpPost httppost = 
new 
HttpPost(url);
            
// 设置参数
            
httppost.setEntity(
new 
UrlEncodedFormEntity(params));
            
// 发送请求
            
HttpResponse httpresponse = hc.execute(httppost);
            
// 获取返回数据
            
HttpEntity entity = httpresponse.getEntity();
            
body = EntityUtils.toString(entity);
            
if 
(entity != 
null
) {
                
entity.consumeContent();
            
}
        

catch 
(UnsupportedEncodingException e) {
            
e.printStackTrace();
        

catch 
(ClientProtocolException e) {
            
e.printStackTrace();
        

catch 
(ParseException e) {
            
e.printStackTrace();
        

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

转自:http://www.oschina.net/code/snippet_54371_1515

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/154999.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 知识网之C++总结

    知识网之C++总结

    2022年1月21日
    37
  • SpringBoot 源码解读

    SpringBoot 源码解读Springboot之前出现的问题Spring框架需要进行大量的配置项目的依赖管理冲突为什么是SpringBootSpringBoot本身并不提供Spring框架的核心特性以及扩展功能,只是用于快速、敏捷地开发新一代基于Spring框架的应用程序。也就是说,它并不是用来替代Spring的解决方案,而是和Spring框架紧密结合用于提升Spring开发者体验的工具。…

    2022年4月30日
    40
  • 查看JAVA API以及JAVA源码的方法

    查看JAVA API以及JAVA源码的方法在java的日常学习中,我们有时候会需要看java的api说明,或者是查看java的源码,使我们更好的了解java,接下来我就来说说如何查看java的api以及java源码对于java的api,一般是在下面的网址中进行查看https://docs.oracle.com/javase/8/docs/api/而对于java的源码,我们现在来演示查看nextLine()的源码:将鼠标…

    2022年7月26日
    1
  • 数据不平衡之SMOTE算法

    数据不平衡之SMOTE算法在企业的数据分析中,很少会遇到正负样本数据比例平衡的状况。通常情况是,绝大多数为正样本,而只有极少数(几个或者十几个)负样本。在这种情况下,不论是用LR,SVM或者基于提升方法的随机森林,直接用该数据集进行学习的效果都不会太好,原因是这些方法的学习结果都会偏向于样本较多的一类。另一个方面,对学习结果进行评估时,假如正样本占95%,负样本仅占5%,这样甚至不需要学习,直接把所有新样本预测为正,准确率

    2022年6月22日
    87
  • MySQL中日期时间类型与格式化「建议收藏」

    MySQL中日期时间类型与格式化「建议收藏」Mysql中常用的几种时间类型有:date、datetime、time、year、timestamp;Datetime:时间日期型,格式是YYYY-mm-ddHH:ii:ss,表示的范围是从1000到9999。但是有零值,0000-00-0000:00:00;Date:日期,就是datetime中的date部分;Time:时间(段),指定的某个区间之间,从-时间到+时间(有负时间表示);T

    2022年4月30日
    48
  • pycharm许可证过期_当前系统license过期

    pycharm许可证过期_当前系统license过期问题描述今天打开PyCharm的时候弹出提示框Yourlicensehasexpired,表示证书到期了,无法继续使用软件。之前使用的证书是在学校的时候注册的学生账号,也就是以.edu.cn结尾的账号。现在需要寻找一种新的认证方式。解决方案PyCharm属于JetBrains的一员,这个系列的软件的认证方式有三种,分别是账号登录、激活码和授权服务器。对于在校的学生用户,…

    2022年8月25日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号