• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

一个HttpClient小程序。。。

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

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

朋友做手机号码生意,需要定期修改大量手机号服务密码,让我帮忙写程序。。。

好吧,开始动工,计划从本地txt文件读取格式化数据,然后用程序去批量提交。

当仁不让的用httpclient了。

先分析需要哪些表单数据

随便填写表单,点击修改,跳转到

查看提交数据,参照html表单,确定提交的数据

 

好了,需求以及清楚了,那么下面开始

第一步:读取号码信息

本地文本格式化成这样,手机号码----旧密码----新密码;

 /**
     * 读取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 做了自己的事情,具体就不研究了。

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap