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

📄 httptest1.java~122~

📁 wbml地址转换
💻 JAVA~122~
字号:
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();
        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("User-Agent", "xiaoxj user agent");
        authpost.setRequestBody(
          new NameValuePair[] {action});
      client.executeMethod(authpost);
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 + -