一個HttpClient 小程序。。。

 

感謝:
http://79140142-qq-com.iteye.com/blog/1399409
http://hi.baidu.com/w8y56f/item/aa35ca3ff175145380f1a74a
--------------------------------------------------------------

朋友作手機號碼生意,須要按期修改大量手機號服務密碼,讓我幫忙寫程序。。。html

好吧,開始動工,計劃從本地txt文件讀取格式化數據,而後用程序去批量提交。java

當仁不讓的用httpclient了。bootstrap

先分析須要哪些表單數據app

隨便填寫表單,點擊修改,跳轉到eclipse

查看提交數據,參照html表單,肯定提交的數據ide

 

好了,需求以及清楚了,那麼下面開始ui

第一步:讀取號碼信息this

本地文本格式化成這樣,手機號碼----舊密碼----新密碼;spa

 /**
     * 讀取15151433862----123456文檔
     * @param f
     */
    private static void findContent(File f){
        String line = null;
        try {
            BufferedReader br = null;
            try {
                br = new BufferedReader(new InputStreamReader(new FileInputStream(f), "gbk"));
            } catch (UnsupportedEncodingException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                line = br.readLine();
                while(line != null){
                  //讀取一行記錄,發送http請求
                    String[] strArr = line.split("----");
                    //System.out.println(strArr[0] +"======="+ strArr[1]+"======="+strArr[2]);
                    try {
                        modify(strArr[0],strArr[1],strArr[2]);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    line = br.readLine();
                    
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        
    }

讀取數據後,調用modif()方法逐行修改,命令行

httpClient更新就是快,網上一大堆3.0之前的老版本,開源項目都是與時俱進的,官網老版本早就下架,

乖乖下載開發包,參考指導,寫了下面這麼一段,好在需求不復雜,等有時間把4.3的tutorial仔細看看

//發送http請求
    private static void modify(String mobile,String oldPwd,String newPwd) throws Exception{
        //得到HttpClient實例
        CloseableHttpClient httpclient = HttpClients.createDefault();
        URI uri = new URIBuilder()
        .setScheme("http")
        .setHost("wap.js.10086.cn")
        .setPath("/actionDispatcher.do")
//        .setParameter("mobile", "15161471125")
//        .setParameter("oldPwd", "123111")
//        .setParameter("newPwd", "123333")
//        .setParameter("newPwdConfirm", "123333")
        .setParameter("mobile", mobile)
        .setParameter("oldPwd", oldPwd)
        .setParameter("newPwd", newPwd)
        .setParameter("newPwdConfirm", newPwd)
        .setParameter("busiNum", "MMFW_MMXG")
        .setParameter("checkOld", "1")
        .setParameter("ver", "s")
        .setParameter("operType", "4")
        .setParameter("pageNum", "MMFW_MMXG")
        .setParameter("confirm_isConfirm", "1")
        .setParameter("fee", "")
        .build();
        
        HttpGet httpget = new HttpGet(uri);
        System.out.println(httpget.getURI());
        CloseableHttpResponse response = httpclient.execute(httpget);
        System.out.println(response.toString());
        try {
        } finally {
            response.close();
        }
    }
public static void main(String[] args) throws Exception {
        
        File filename = new File("D:/號碼.txt");
        findContent(filename);
    }

好了,大功告成,跑一下是能夠的,

第三步,導出jar包,這裏遇到些問題,百度了下,

爲何export-->runnable jar file的launch configuration沒有東西能夠選擇?

對於這個問題,網上答案少之又少,我來製造些內容吧。

爲 什麼MyEclipse8.5的export-->runnable jar file-->的launch configuration裏面沒有能夠選擇的東西了,實際上是要把你要打包成jar文件的工程的main方法運行一次,好比main方法在A類裏,運行一 次A就有了

   我是看見下面這段,試了一下弄出來的,喜歡的人仔細研究一下  Creating a Java application launch configuration

When you choose Run >Run As >Java Application to launch your class, you are running your class using a generic Java Application launch configuration that derives most of the launch parameters from your Java project and your workbench preferences.  In some cases, you will want to override the derived parameters or specify additional arguments.

You do this by creating your own Java Application launch configuration. 

  1. Select or from the workbench menu bar.  This opens a dialog that lets you create, modify, and delete launch configurations of different types.
  2. Select Java Application in the left hand list of launch configuration types, and press the New button in the toolbar. This will create a new launch configuration for a Java application. The tabs on the right hand side allow you control specific aspects of the launch.
  • The Main tab defines the class to be launched. Enter the name of the project containing the class to launch in the project field, and the fully qualified name of the main class in the Main class field. Check the Stop in main checkbox if you want the program to stop in the main method whenever the program is launched in debug mode.
    Note:  You do not have to specify a project, but doing so allows a default classpath, source lookup path, and JRE to be chosen.
  • The Arguments tab defines the arguments to be passed to the application and to the virtual machine (if any). You can also specify the working directory to be used by the launched application.
  • The JRE tab defines the JRE used to run or debug the application. You can select a JRE from the already defined JREs, or define a new JRE.
  • The Classpath tab defines the location of class files used when running or debugging an application. By default, the user and bootstrap class locations are derived from the associated project's build path. You may override these settings here.
  • The Source tab defines the location of source files used to display source when debugging a Java application. By default, these settings are derived from the associated project's build path. You may override these settings here.
  • The Environment tab defines the environment variable values to use when running or debugging a Java application. By default, the environment is inherited from the Eclipse runtime. You may override or append to the inherited environment.
  • The Common tab defines general information about the launch configuration.  You may choose to store the launch configuration in a specific file and specify which perspectives become active when the launch configuration is launched.

 

 

成功導出runnable jar  file,cmd打開命令行,找到jar包路徑,運行 java -jar test.jar;
 

 

而後朋友不會java,,將jar包轉成exe程序給他用

jar轉exe轉換器(jar2exe)(一款能夠將jar文件轉換成exe)下面的連接很詳細

http://jingyan.baidu.com/article/00a07f38aad55182d128dc4c.html

 

 

 

 

 

開始的時候是打成jar,而後運行的時候報錯

運行jar 提示 Failed to load Main-Class manifest attribute from

  緣由描述:MANIFEST.MF文件中的Main-Class配置不正確或格式不正確

 

 檢查方式:以WinRarR的方式打開jar包,如圖所示, 

 

 點擊進入箭頭所指的META-INF文件夾

 

  將MANIFEST.MF拷貝出來進行編輯

 

  編輯內容,示例以下:

 

  注意好以上三個方面就應該沒什麼問題了

  將編譯好的MANIFEST.MF文件依然以WinRaR的打開方式覆蓋已有的,或添加到META-INF文件夾目錄下,完事...

 

 

而後從網上找了下eclipse中 jar file 和runnable jar file的區別

http://liuzhen.liujie.blog.163.com/blog/static/12755615620130801054448/

 從eclipse中export 的jar file,僅僅是把.class打包了。因此執行這種jar file須要用 java -cp .;ch04.jar com.thnkjava.ch04, 若是你還應用到另外的lib庫,你必須在cp裏說明,也就是 java -cp .;ch04.jar;lib01.jar;lib02.jar com.thnkjava.ch04 來執行。事實上能夠發現ch04.jar也在cp裏面,說明ch04.jar就是lib庫,最後的參數指明瞭要被執行的類名。

若是從 eclipse裏export出的是 runnable jar file,那麼個執行這個jar包的時候是不須要指明哪一個類的,直接這樣執行 java -jar ch04.jar。緣由就是jar包中的MANIFEST.MF內容不一樣。 runnable jar包中指明哪一個類先執行,因此你能夠用 java -jar ch04.jar來執行你想要執行的代碼,而沒必要指明具體哪一個類。這個你能夠打開 jar包查看MANIFEST.MF的區別,一目瞭然。

生成runnable jar file時,有兩個選項,Extract required libraries into generated JAR 和 package equired libraries into generated JAR。 前者是把你用到的.class 文件提取出來,後者則是把你所須要的全部jar包都打進一個包裏。二者的MANIFEST.MF文件內容也有所不一樣,這應該是eclipse形成 的,IDE 作了本身的事情,具體就不研究了。

相關文章
相關標籤/搜索