`
lenj
  • 浏览: 37425 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
最近访客 更多访客>>
社区版块
存档分类
最新评论

自己用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("&nbsp;"));
				city =pc.substring(pc.lastIndexOf("&nbsp;")+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(); 
		}
		
	}
		
	}


分享到:
评论

相关推荐

    httclient 中文教程

    HttpClient是Apache基金会开发的一个HTTP客户端库,用于在Java应用程序中执行HTTP请求。这个库提供了丰富的功能,包括HTTP/1.1和HTTP/2协议支持、连接管理、重试策略、缓存机制等,使得开发者可以方便地进行网络通信...

    httpclient需要的jar包,合订版(httclient.jar,commons-codec,commons-logging)

    httpclient运行需要的三个jar包 HttpClient 可以在http://jakarta.apache.org/commons/httpclient/downloads.html下载 HttpClient 用到了 Apache Jakarta common 下的子项目 logging,你可以从这个地址...

    自己写的一个spider提取url并保存的线程类

    运用最新Jsoup技术开发,比httClient容易多了

    java爬虫,HttpGet,HttClient

    Java爬虫是编程领域中一个有趣的主题,它允许开发者通过编写代码来自动抓取和处理互联网上的信息。在这个场景中,我们关注的是使用Java来爬取并下载一千张美女图片。我们将深入探讨Java爬虫的基本原理,HttpClient库...

    C# WinForm下载上传文件显示进度条

    首先,我们需要创建一个WinForm窗口,包含一个按钮(用于启动下载或上传操作)、一个进度条控件(ProgressBar)以及可能的文本框或者标签来显示相关信息。在VS2010中,可以通过设计视图直接拖放这些控件到窗体上。 ...

    http-client-HTTP客户端的类型和特征-Rust开发

    API文件| 发布| ... API文件| 发布| 参与安装安装了add add的运行:$ cargo add http-client安全对于非Wasm...请查看我们的“供稿”指南,并查看其中的一些问题:标为“良好的第一期”的问题标为“需要帮助”的问题Li Li

    HttpClient通过Post上传文件的实例代码

    在网上寻找之后,发现是使用HttClient来进行响应的操作,起初尝试多次依然不能传递参数和传递文件,后来发现时因为当使用HttpClient时,不能使用request.getParameter()对普通参数进行获取,而要在服务器端使用...

    C#中HttpWebRequest、WebClient、HttpClient的使用详解

    以下是一个使用WebClient的POST请求示例: ```csharp public static string WebClientPost(string url, string postDataStr) { var client = new WebClient(); client.Headers[HttpRequestHeader.ContentType] = ...

    简单的网站检测工具,发现网站死掉,可自动重启动web

    本文将详细介绍一个名为"简单的网站检测工具"的程序,它能够自动检测网站状态并重新启动Web服务器,以保持服务的正常运行。 这个工具的主要功能在于其定时扫描机制。它会定期(例如每分钟、每小时)访问预先设定的...

    httpClient

    commons-httpclient-2

Global site tag (gtag.js) - Google Analytics