⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httptest1.java~167~

📁 wbml地址转换
💻 JAVA~167~
字号:
package httpclient;

import java.io.IOException;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.methods.*;

public class HttpTest1 {
    public HttpTest1() {
    }

    public static void main(String[] args) {
        HttpTest1 httpTest = new HttpTest1();
        String urlStr = httpTest.test1();
        httpTest.postTest(urlStr);
//        for (int i = 0; i < 30; i++) {
//            try {
//                System.out.println("********************************");
//                urlStr = httpTest.test2(urlStr);
//                Thread.sleep(1000);
//            } catch (InterruptedException ex) {
//            }
//        }
    }

    public String test1() {
        String retStr = "";
        try {
            HttpClient client = new HttpClient();
            GetMethod get = new GetMethod("http://localhost/first.jsp?r=494");

//            UsernamePasswordCredentials upc =
//                    new UsernamePasswordCredentials("foo", "bar");

            //client.getState().setCredentials(null, null, upc);

//            get.setDoAuthentication(true);

            //client.setConnectionTimeout(60000);

            client.executeMethod(get);
            String str = get.getResponseBodyAsString();
            System.out.println(str.substring(str.indexOf("<card id")).substring(
                    50, 100));
            int l1 = str.indexOf("href=");
            str = str.substring(l1 + "href=".length() + 1);
            l1 = str.indexOf("href=");
            str = str.substring(l1 + "href=".length() + 1);

            //System.out.println("str1 is:  " + str);
            int l2 = str.indexOf(">");
            str = str.substring(0, l2 - 1);

            retStr = "http://localhost/" + str;
            System.out.println("" + retStr);
            get.releaseConnection();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return retStr;
    }

    public String test2(String argStr) {
        String urlStr = "";
        try {
            HttpClient client = new HttpClient();
            GetMethod get = new GetMethod("http://localhost/first.jsp?r=494");

//            UsernamePasswordCredentials upc =
//                    new UsernamePasswordCredentials("foo", "bar");

            //client.getState().setCredentials(null, null, upc);

//            get.setDoAuthentication(true);

            //client.setConnectionTimeout(60000);

            GetMethod redirect = new GetMethod(argStr);
            client.executeMethod(redirect);
            String str = redirect.getResponseBodyAsString();
            String str2 = str;
            //System.out.println("str: \n" + str);
            int l1 = str.indexOf("href=");
            str = str.substring(l1 + "href=".length() + 1);
            l1 = str.indexOf("href=");
            str = str.substring(l1 + "href=".length() + 1);

            //System.out.println("str1 is:  " + str);
            int l2 = str.indexOf(">");
            str = str.substring(0, l2 - 1);

            urlStr = "http://localhost/" + str;
            System.out.println("" + urlStr);
            System.out.println(str2.substring(str2.indexOf("<card id")).
                               substring(
                                       50, 100));
            System.out.println("Redirect: " + redirect.getStatusLine().toString());
            // release any connection resources used by the method
            redirect.releaseConnection();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return urlStr;
    }

    public void postTest(String argStr) {
        HttpClient client = new HttpClient();
        PostMethod authpost = new PostMethod(argStr);
        NameValuePair action = new NameValuePair("action", "login");
        NameValuePair url = new NameValuePair("url", "/index.html");
        NameValuePair userid = new NameValuePair("UserId", "userid");
        NameValuePair password = new NameValuePair("Password", "password");
        authpost.setRequestBody(
                new NameValuePair[] {action, url, userid, password});
        try {
            Header header = new Header();
            header.setName("User-Agent");
            header.setValue("cjw agent");
            authpost.addRequestHeader(header);
            Header header2 = new Header();
            header2.setName("X-Up-Calling-Line-ID");
            header2.setValue("13574179835");
            authpost.addRequestHeader(header2);

            NameValuePair nv = new NameValuePair();
            nv.setName("User-Agent");
            nv.setValue("xxj");

            client.executeMethod(authpost);
        } catch (IOException ex) {
        }

        System.out.println("Login form post: " +
                           authpost.getStatusLine().toString());
// release any connection resources used by the method
        authpost.releaseConnection();

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -