httpclient开发
来源:互联网 发布:剑灵剑士捏脸数据大全 编辑:程序博客网 时间:2024/06/02 07:27
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;
}
}
- httpclient开发
- Java 客户端HttpClient 开发
- HttpClient+AsyncTask开发App
- Android开发之HttpClient
- 安卓开发-HttpClient使用
- Apache HttpClient 4.3开发指南
- 《Apache HttpClient 4.3开发指南》
- 《Apache HttpClient 4.3开发指南》
- 安卓开发-HttpClient使用
- 安卓开发-HttpClient使用
- Apache HttpClient 4.3开发指南
- Apache HttpClient 4.3开发指南
- 《Apache HttpClient 4.3开发指南》
- Android开发之HttpClient初步
- Apache HttpClient 4.3开发指南
- Apache HttpClient 4.3开发指南
- HttpClient
- HttpClient
- 亦舒的话,我的感触...
- WinCE下重启系统的方法
- UpdateWindow和Invalidate的区别
- asp.net页面事件重写问题
- 在Jsp/Servlet中页面重新定向总汇
- httpclient开发
- 开发人员一定要加入收藏夹的网站收藏
- SaaS 软件服务化
- 查找两个字符串的最长公共子串的Javascript函数
- inezha(哪吒网)这个思路很不错
- 开始鄙视我自己
- FLEX实践—Application与Module之通信
- 无指针,不java——40岁偷练java之内功心法(3)
- 免费调节阀流量系数(KV/CV)计算选型软件