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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • QT学习笔记15绘图和绘图设备

    QT学习笔记15绘图和绘图设备1QPainterQt的绘图系统允许使用相同的API在屏幕和其它打印设备上进行绘制。整个绘图系统基于QPainter,QPainterDevice和QPaintEngine三个类。QPainter用来执行绘制的操作;QPaintDevice是一个二维空间的抽象,这个二维空间允许QPainter在其上面进行绘制,也就是QPainter工作的空间;QPaintEngine提供了画笔(Q

    2022年4月18日
    42
  • CSS在IE6、7、8中的差别

    CSS在IE6、7、8中的差别

    2021年6月20日
    139
  • 用websocket实现实时聊天功能

    用websocket实现实时聊天功能最近想实现网页版的仿QQ聊天工具,本来想用ajax实现的,但是一想到要一直轮询,就感觉有点蠢。后来在网上找到了websocket相关的资料,就拿来跟大家分享下(不是很熟练,现在只实现了群聊,单聊的前端不会写了。但可以跟大家说说思路)。服务器端代码:首先要创建类WebSocketConfig实现ServerApplicationConfig接口,ServerApplicationConfig项目…

    2022年10月21日
    4
  • win10命令行强制删除文件_win10cmd强制删除文件夹

    win10命令行强制删除文件_win10cmd强制删除文件夹提醒:以下方法文件永久删除,常规方法无法恢复,慎用,慎用,慎用针对电脑中不知什么软件生成的无用文件,使用修改文件夹属性的可视化方法,试过多次都没有成功,后通过执行命令行删除文件。步骤如下:(1)

    2022年8月1日
    18
  • GloVe模型_nerlove模型

    GloVe模型_nerlove模型系列目录(系列更新中)第二讲cs224n系列之word2vec&amp;amp;amp;amp;amp;amp;词向量word2vec进阶之skim-gram和CBOW模型(HierarchicalSoftmax、NegativeSampling)第三讲cs224n系列之skip-pram优化&amp;amp;amp;amp;amp;amp;GlobalVectorbyManning&amp;amp;amp;amp;amp;amp;

    2025年11月24日
    3
  • js 数组插入删除[通俗易懂]

    js 数组插入删除[通俗易懂]常用的方法是遍历数组,然后使用splice()删除这里我们使用es6中findIndex()查找,然后删除functiondeleteFromArray(arr,compare){constindex=arr.findIndex(compare)if(index>-1){arr.splice(index,1)}}插入数据functioninsertArray(arr,val,compare,maxLen){//返回位置consti

    2022年9月30日
    2

发表回复

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

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