自己用HTTCLIENT写的一个小程序

实现功能:匹配A表中是否B中的手机号码,如果 没有就从B查询出来,然后把所查询到的手机号码拿到HTTP://WWW。IP138.COM查询其归属地城市,区号等,把得到的值再插入A表!
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class HttpClientTutorial {    

private static String url = "http://www.ip138.com:8080/search.asp?mobile=1375828&action=mobile&B1=%B2%E9+%D1%AF";

public static void main(String[] args) throws SQLException {

HttpClient client = new HttpClient();
PostMethod method = new PostMethod(url);
// method.setRequestHeader("POST","/search.asp HTTP/1.1");
method.setRequestHeader("Accept"," image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,");
method.setRequestHeader("Referer"," http://www.ip138.com:8080/search.asp");
method.setRequestHeader("Accept-Language","zh-cn");
method.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
method.setRequestHeader("Accept-Encoding","gzip, deflate");
method.setRequestHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
method.setRequestHeader("Host","www.ip138.com:8080");
// method.setRequestHeader("Content-Length","45");
method.setRequestHeader("Connection","Keep-Alive");
method.setRequestHeader("Cache-Control","no-cache");
// method.setRequestHeader("Cookie","rtime=2; ltime=1210316485984; cnzz_eid=94280797-; cnzz_a396332=3;ASPSESSIONIDSACATRTT=MEMCJOMBMNDMPAODCNFPLEIA");

Connection conn = dbConn.getConn();

String sql="select distinct left(desttermid,7) from submitsms_log5 a where left(a.desttermid,7) not in (select mseg from UT_COMM_MOBILE_SECTION)";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();

while(rs.next()) {
String Mseg="";
Mseg = rs.getString(1);
if(Mseg!=null&&Mseg.trim().length()!=0){
Mseg = rs.getString(1);

NameValuePair[] data = { new NameValuePair("action", "mobile"),
new NameValuePair("mobile",Mseg)};
method.setRequestBody(data);

// System.out.println(Mseg);

try {
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
//System.err.println("Method failed: " + method.getStatusLine());
}
byte[] responseBody = method.getResponseBody();

String htmlPage = new String(responseBody);
// System.out.println("htmlPage="+htmlPage); //打印出源码
int temppoint=htmlPage.indexOf("您查询的手机号码段");
int str1=htmlPage.indexOf("卡号归属地");
int str2=htmlPage.indexOf("卡 类 型");
int str3=htmlPage.indexOf("区 号");
int str4=htmlPage.indexOf("邮 编");

Mseg=htmlPage.substring(temppoint+56, temppoint+63);

String prov="";//省份
String city="";//城市
String cardtype="";//卡类型
String regcode="";//区号
String ecity="";//城市拼音
String pc="";

if(str2-88<=str1+60){
prov = htmlPage.substring(str1+52, str1+54);
// city=htmlPage.substring(str1+60,str2-88 ); 当是未知的时候没有城市这个
cardtype=htmlPage.substring(str2+62,str2+64);
regcode=htmlPage.substring(str3+42,str4-69);//
ecity=CnToSpell.getFullSpell(city);//把城市名称转换成拼音!

}else {
//prov = htmlPage.substring(str1+52, str1+54);
//city=htmlPage.substring(str1+60, str2-88);
pc=htmlPage.substring(str1+52,str2-88);//省份和城市字符串
prov =pc.substring(0, pc.lastIndexOf(" "));
city =pc.substring(pc.lastIndexOf(" ")+6,pc.length());//+6加的是HTML空格的长度
cardtype=htmlPage.substring(str2+62,str2+68);
regcode=htmlPage.substring(str3+42,str4-69);
ecity=CnToSpell.getFullSpell(city);//把城市名称转换成拼音!
}

System.out.println("pc="+pc);
System.out.println("Mseg="+Mseg);
System.out.println("prov="+prov);
System.out.println("city="+city);
System.out.println("cardtype="+cardtype);
System.out.println("regcode="+regcode);
System.out.println("ecity="+ecity);


execInsertLog(Mseg, prov, city, cardtype, regcode);


} catch (HttpException e) {
System.err.println("Fatal protocol violation: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();

} finally {

method.releaseConnection();

}
}//判断MSEG是否为空.
}
}
public static void execInsertLog(String Mseg,String prov,String city,String cardtype,String regcode){
try {
Connection conn = dbConn.getConn();

String sql="insert into UT_COMM_MOBILE_SECTION(Mseg,prov,city,cardtype,regcode) values(?,?,?,?,?)";

PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1,Mseg);
stmt.setString(2,prov);
stmt.setString(3,city);
stmt.setString(4,cardtype);
stmt.setString(5,regcode);
stmt.execute();
} catch (Exception e) {
e.printStackTrace();
}

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值