java实现post get 传参数url
来源:互联网 发布:小米笔记本office软件 编辑:程序博客网 时间:2024/05/22 10:25
/**
*
*/
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 实现post get 请求url
- java实现url访问(get,post)
- java实现:http协议get和post方法的url参数请求响应及交互
- Java获得GET和POST请求URL和参数列表
- java 实现向指定URL发送get 和 post 请求
- java获取get,post参数
- java如何得到GET和POST请求URL和参数列表
- java如何得到GET和POST请求URL和参数列表
- java如何得到GET和POST请求URL和参数列表
- java如何得到GET和POST请求URL和参数列表
- java如何得到GET和POST请求URL和参数列表
- java如何得到GET和POST请求URL和参数列表
- java如何得到GET和POST请求URL和参数列表
- XCODE jsonMODEL对换数据
- 欢迎使用CSDN-markdown编辑器
- 【转载】Java泛型中E、T、K、V等的含义
- eclipse validating 卡着一直不动
- openstack---neutron commands list
- java实现post get 传参数url
- 怎样使用MindManager 2017中时间轴
- TextView -- 跑马灯
- Android混合开发相关文章链接
- illumination-aware age progression -> flow and illumination
- header中Content-Disposition的作用与使用方法
- Retrofit用法详解之一(无参数get请求)
- 微信公众号接入指南——成为开发者配置
- 关于Eclipse使用.9.png图片出现黑边问题