httpclient开发
1)引入httpclient开发所需要的jar包
2)实例一:
package testHttpClient;
import java.io.IOException;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
class fection
{
static public void main(String []args) throws IOException
{
int i = 0;
while(i<2)//做人要厚道
{
String PostURL=("http://sms.api.bz/fetion.php?username=&password=&sendto=&message=");
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod(PostURL);
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"GBK");//发送中文,避免乱码
postMethod.addParameter("username","13640815106");
postMethod.addParameter("password","不告诉你");
postMethod.addParameter("sendto","13640815106");//立方
postMethod.addParameter("message","试试看,这是第"+Integer.toString(i+1)+"条");
client.setConnectionTimeout(10000);
client.executeMethod(postMethod);
i++;
}
System.out.println("OK!");
}
}
2)实例二:
package testHttpClient;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
public class Phone {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Phone ph=new Phone();
System.out.println("phone");
String adder=ph.getArea("13933600146");
System.out.println(adder);
System.out.println(adder.length());
}
/**
* 使用GET方式提交数据
* @param phone
* @return
*/
private static HttpMethod getGetMethod(String phone){
return new GetMethod("/features/baibaosearch.jsp?query="+phone);
}
/**
* 使用POST方式提交数据
* @return
*/
private static HttpMethod getPostMethod(String phone){
PostMethod post = new PostMethod("/features/baibaosearch.jsp");
NameValuePair simcard = new NameValuePair("query",phone);
post.setRequestBody(new NameValuePair[] { simcard});
return post;
}
public String getArea(String phone){
String result = "";
try{
HttpClient client = new HttpClient();
// client.getHostConfiguration().setHost("www.imobile.com.cn", 80, "http");
client.getHostConfiguration().setHost("www.sogou.com",80,"http");
// HttpMethod method = getPostMethod(phone);//使用POST方式提交数据
HttpMethod method = getPostMethod(phone);//使用POST方式提交数据
client.executeMethod(method);
//打印服务器返回的状态
System.out.println(method.getStatusLine());
//打印结果页面
String response = method.getResponseBodyAsString();
// String response = new String(method.getResponseBodyAsString().getBytes("8859_1"));
//打印返回的信息
int index = response.indexOf("手机号");
response = response.substring(index,response.length());
int index2 = response.indexOf("数据来源");
response = response.substring(0, index2);
result = response;
method.releaseConnection();
}catch(Exception e){
e.printStackTrace();
result = "异常 : "+e.getMessage();
}
return result;
}
}
转载于:https://www.cnblogs.com/sunxiangfu/archive/2009/07/01/4261129.html
httpclient开发相关推荐
- 用httpclient开发的在线自动抢订火车票系统(铁老大不给力,哥给力)
前两天女友要在线订火车在票,一直都没有办法订到票,最后没办法,便给她写了个自动抢票的脚本,可周边的朋友听她说通过软件订到票了,都先后向她要软件,可原来的脚本只是基于控制台输入,这样给别人也没法用,兴趣 ...
- android-async-http 源码分析
android-async-http是一个为Android量身定制的异步HTTP请求框架,它基于Apache的HttpClient开发.本文针对v1.4.6版本进行分析. 1. 功能介绍 androi ...
- .NET Core微服务之服务间的调用方式(REST and RPC)
一.REST or RPC ? 1.1 REST & RPC 微服务之间的接口调用通常包含两个部分,序列化和通信协议.常见的序列化协议包括json.xml.hession.protobuf.t ...
- .net的retrofit--WebApiClient底层篇
前言 本篇文章的内容是WebApiClient底层说明,也是WebApiClient系列接近尾声的一篇文章,如果你没有阅读过之前的的相关文章,可能会觉得本文章的内容断层 库简介 WebApiClien ...
- .net的retrofit--WebApiClient库深入篇
前言 本篇文章的内容是对上一篇.net的retrofit--WebApiClient库的深层次补充,你可能需要先阅读上一篇才能理解此篇文章.本文将详细地讲解WebApiClient的原理,结合实际项目 ...
- net的retrofit--WebApiClient库
# 库简介 WebApiClient是开源在github上的一个httpClient客户端库,内部基于HttpClient开发,是一个只需要定义c#接口(interface),并打上相关特性,即可异步 ...
- 转: Springboot — 用更优雅的方式发HTTP请求(RestTemplate详解)
转自: Springboot - 用更优雅的方式发HTTP请求(RestTemplate详解) - Java知音号 - 博客园RestTemplate是Spring提供的用于访问Rest服务的客户端, ...
- Springboot — 用更优雅的方式发HTTP请求(RestTemplate详解)
RestTemplate是Spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率. 我之前的HTTP开发是用ap ...
- restTemplate访问接口
后端技术精选 每天推送精选技术好文,涉及Java.python.Linux及MySQL,欢迎关注微信公众号:后端技术精选 随笔 - 52, 文章 - 0, 评论 - 50, 引用 - 0 Spring ...
最新文章
- 神经网络的5种常见求导,附详细的公式过程
- android 常用类
- 程序员面试题精选100题(42)-旋转数组的最小元素[算法]
- JavaScript的数据结构与算法(三) —— 单向链表
- java清理垃圾_垃圾清理势在必行——Java垃圾收集算法
- IE7下JSON不能有多余的逗号,IE8下创建IMG节点的BUG
- 前端直接访问登录页面报错_如何实现登录、URL和页面按钮的访问控制
- Word2vec安装使用
- C语言坐标打飞机,C语言实现打飞机小游戏
- python---面向对象1
- Gartner点将分布式文件存储,浪潮存储缘何一鸣惊人?
- dcos - 安装docker
- 台式电脑win7旗舰版 怎么调节屏幕亮度 显示器太亮了!
- [计算机一级MS备考]
- iOS 联系在线客服功能
- 企业如何构建内部开发者平台?
- Web前端(一)HTML超文本标记语言
- MATLAB数学建模 线性方程式与线性系统
- linux查看图形化系统版本,查看 Linux 系统版本信息
- Marvelous Designer 10 MAC 版 MD 10 下载 ( 支持 Big Sur )