java 实现post get 请求url
来源:互联网 发布:钉钉侵犯员工隐私知乎 编辑:程序博客网 时间:2024/05/16 19:50
/**
*
*/
package com.byzk.p2p.thread;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
/*** <p>Title:HttpClientTest </p>
* <p>Description:使用httpClient 包 实现post get 提交 </p>
* <p>Company:xbd </p>
* @author:Administrator
* @date:2016年11月7日上午11:53:10 */
public class HttpClientTest {
/**
*
* <p>Description:get 请求方式连接 </p>
* <p>Company:xbd </p>
* @author:lilei
* @date:2016年11月7日下午12:31:48
*/
public void GetTest(String url) {
HttpClient httpclient = new HttpClient();
// 创建get实例
GetMethod getmethod = new GetMethod(url);
getmethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
try {
// 执行getMethod
int statucode = httpclient.executeMethod(getmethod);
if (statucode != HttpStatus.SC_OK) {
System.err.println("method failed" + getmethod.getStatusLine());
}
// 读取内容
byte[] responseBody = getmethod.getResponseBody();
// 处理内容
System.out.println(new String(responseBody));
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
getmethod.releaseConnection();
}
}
public void postTest(String url) {
HttpClient httpclient = new HttpClient();
//初始化post
PostMethod postmethod = new PostMethod(url);
List<NameValuePair> list = new ArrayList<NameValuePair>();
NameValuePair pair = new NameValuePair("user_id", "1213");
list.add(pair);
postmethod.setRequestBody((NameValuePair[]) list.toArray());
//执行postmethod
try {
int statuCode = httpclient.executeMethod(postmethod);
if(statuCode == HttpStatus.SC_MOVED_PERMANENTLY || statuCode == HttpStatus.SC_MOVED_TEMPORARILY){
Header header = postmethod.getRequestHeader("location");
String location="";
if(header!=null){
location = header.getValue();
System.out.println("the page was redirect to "+ location);
}else{
System.out.println("location value is null");
}
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
*
*/
package com.byzk.p2p.thread;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
/*** <p>Title:HttpClientTest </p>
* <p>Description:使用httpClient 包 实现post get 提交 </p>
* <p>Company:xbd </p>
* @author:Administrator
* @date:2016年11月7日上午11:53:10 */
public class HttpClientTest {
/**
*
* <p>Description:get 请求方式连接 </p>
* <p>Company:xbd </p>
* @author:lilei
* @date:2016年11月7日下午12:31:48
*/
public void GetTest(String url) {
HttpClient httpclient = new HttpClient();
// 创建get实例
GetMethod getmethod = new GetMethod(url);
getmethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
try {
// 执行getMethod
int statucode = httpclient.executeMethod(getmethod);
if (statucode != HttpStatus.SC_OK) {
System.err.println("method failed" + getmethod.getStatusLine());
}
// 读取内容
byte[] responseBody = getmethod.getResponseBody();
// 处理内容
System.out.println(new String(responseBody));
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
getmethod.releaseConnection();
}
}
public void postTest(String url) {
HttpClient httpclient = new HttpClient();
//初始化post
PostMethod postmethod = new PostMethod(url);
List<NameValuePair> list = new ArrayList<NameValuePair>();
NameValuePair pair = new NameValuePair("user_id", "1213");
list.add(pair);
postmethod.setRequestBody((NameValuePair[]) list.toArray());
//执行postmethod
try {
int statuCode = httpclient.executeMethod(postmethod);
if(statuCode == HttpStatus.SC_MOVED_PERMANENTLY || statuCode == HttpStatus.SC_MOVED_TEMPORARILY){
Header header = postmethod.getRequestHeader("location");
String location="";
if(header!=null){
location = header.getValue();
System.out.println("the page was redirect to "+ location);
}else{
System.out.println("location value is null");
}
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
0 0
- java 实现post get 请求url
- java实现URL带参数请求(get/post)
- java实现URL带参数请求(get/post)
- java实现URL带参数请求(get/post)
- java 实现向指定URL发送get 和 post 请求
- java如何实现URL带参数请求(get/post)
- Java 请求(Post/Get) Url链接
- java实现post,get请求
- java实现GET/POST请求
- java实现post get 传参数url
- java实现url访问(get,post)
- java实现:http协议get和post方法的url参数请求响应及交互
- java socket 实现get post 请求
- Java实现http get post请求
- Http POST与GET请求JAVA实现
- Java实现模拟发送POST、GET请求
- Java实现get或post请求
- Java实现http(post、get)请求
- 安卓自定义View进阶-事件分发机制详解
- Archlinux下安装和配置zsh
- android developer tiny share-20161026
- LeetCode403. Frog Jump
- 欢迎使用CSDN-markdown编辑器
- java 实现post get 请求url
- redis.conf详解之基本配置
- 小白笔记--------------leetcode(344. Reverse String)
- Xutils3的使用
- swift 构造函数 与便利构造函数
- 关于虚函数的整理(待续)
- 复用类-继承、组合、代理
- 多线程技术
- HTML