java htttp 登录返回cookie 通过cookie 访问其他资源


import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by yaoyao on 16/3/19.
 */
public class test0319 {


    int countUrl=0;


    public static void main(String[] args) {

        try {
//            String cookie = new test0319().login();
            new test0319().getOneHtml("http://192.168.1.103:8090/front/sku/list","utf-8","[rememberMe=deleteMe; Path=/front; Max-Age=0; Expires=Fri, 18-Mar-2016 16:15:23 GMT, JSESSIONID=68683D96F61B34C1CDFB5431ABA5B4BA; Path=/front/; HttpOnly]");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }




    public String getOneHtml(String htmlurl,String encoding,String cookie) throws IOException, InterruptedException
    {

        //最多重复请求5次,用来反爬的
        if(countUrl==5){
            countUrl=0;
            return "0";
        }
        //System.out.println(cookie);
        StringBuilder content = new StringBuilder();
        HttpURLConnection httpConn = null;
        try
        {
            URL url = new URL(htmlurl);

            httpConn = (HttpURLConnection) url.openConnection();
            //头设置,get方法

            HttpURLConnection.setFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.setRequestProperty("User-Agent","Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36");
            httpConn.setRequestProperty("Connection","keep-alive");
            httpConn.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml");
            httpConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            httpConn.setRequestProperty("cookie",cookie);
            httpConn.setRequestProperty("Cache-control","no-cache, no-store");
            httpConn.setRequestProperty("Host","http://192.168.1.103:8090");
            httpConn.setConnectTimeout(20000);
            httpConn.setReadTimeout(20000);
            // logger.info(httpConn.getResponseMessage());
            BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), encoding));打开连接,获取内容
            String temp;
            while ((temp = in.readLine()) != null)
            //替换点一些无用到符号
            {
                content.append(temp);
            }
            in.close();
            httpConn.disconnect();

        }
        catch (final MalformedURLException me)
        {
            System.out.println("url不存在!");
            me.getMessage();
            throw me;
        }
        catch (final FileNotFoundException me)
        {
            System.out.println(htmlurl+"反爬启动");
            return "0";
        }
        catch (final IOException e)
        {
            e.printStackTrace();
            System.out.println("反爬启动:"+htmlurl+"次数:"+countUrl++);
            httpConn.disconnect();
            Thread.sleep(20000);
            return this.getOneHtml(htmlurl, encoding,cookie);
        }

        //System.out.println(sb);
        countUrl=0;
        httpConn.disconnect();

        System.out.println(content.toString());
        return content.toString();

    }

    public String login() throws MalformedURLException, InterruptedException {

        String htmlurl = "http://192.168.1.103:8090/front/login";
        HttpURLConnection httpConn = null;
        String cookie = "";
        try {
            URL url = new URL(htmlurl);

            httpConn = (HttpURLConnection) url.openConnection();

            HttpURLConnection.setFollowRedirects(true);
            httpConn.setRequestMethod("POST");
            httpConn.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36");
            httpConn.setRequestProperty("Connection", "keep-alive");
            httpConn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml");
            httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            httpConn.setRequestProperty("Cache-control", "no-cache, no-store");
            httpConn.setRequestProperty("Host", "http://192.168.1.103:8090/front");
            //httpConn.setRequestProperty("Referer","https://www.linkedin.com/uas/login?session_redirect=http://www.linkedin.com/profile/view?id=222323610&authType=name&authToken=fcEe");
            //post方法,重定向设置
            httpConn.setDoOutput(true);
            httpConn.setDoInput(true);
            httpConn.setUseCaches(false);
            httpConn.setInstanceFollowRedirects(false);
            //写入,post方法必须用流写入的方式传输数据
            StringBuffer str_buf = new StringBuffer(4096);
            OutputStream os = httpConn.getOutputStream();
            str_buf.append("username").append("=").append("13062779665").append("&");
            str_buf.append("password").append("=").append("111111").append("&");
            //str_buf.append("session_redirect").append("=").append(redictURL);
            os.write(str_buf.toString().getBytes());
            os.flush();
            os.close();
            httpConn.setConnectTimeout(20000);
            httpConn.setReadTimeout(20000);
            //获取重定向和cookie
            //String redictURL= httpConn.getHeaderField( "Location" );
            //System.out.println("第一次请求重定向地址 location="+redictURL);

            //获取cookie
            Map<String, List<String>> map = httpConn.getHeaderFields();
            //System.out.println(map.toString());
            Set set = map.keySet();
            for (Iterator iterator = set.iterator(); iterator.hasNext(); ) {
                String key =  String.valueOf(iterator.next());
                if (key != null) {
                    if (key.equals("Set-Cookie")) {
                        System.out.println("key=" + key + ",开始获取cookie"+map.get("Set-Cookie").toString());
                        List<String> list = map.get(key);
                        for (String str : list) {
                            String temp = str.split("=")[0];
                            //System.out.println(temp);

                            //cookie包含到信息非常多,调试发现登录只需这条信息
//                            if (temp.equals("li_at")) {
                                cookie = str;
                                return cookie;
//                            }

                        }
                    }
                }

            }
            httpConn.disconnect();

        } catch (final MalformedURLException me) {
            System.out.println("url不存在!");
            me.getMessage();
            throw me;
        } catch (final FileNotFoundException me) {
            System.out.println(htmlurl + "反爬启动");
            return "0";
        } catch (final IOException e) {
            e.printStackTrace();
            System.out.println("反爬启动:" + htmlurl + "次数:" + countUrl++);
            httpConn.disconnect();
            Thread.sleep(20000);
            return login();
        }
        System.out.println(cookie);
        return cookie;
}}

转载于:https://www.cnblogs.com/yaoyao66123/p/5297775.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java实现异步批量发送HTTP请求数据至服务端可以采用Java的异步框架,如Spring Boot中的异步机制来实现。 具体实现步骤如下: 1. 引入相关依赖:在pom.xml文件中添加Spring Boot的异步依赖,如下所示: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-async</artifactId> </dependency> ``` 2. 创建异步任务:使用@Async注解创建异步任务,如下所示: ``` @Service public class AsyncService { @Async public CompletableFuture<String> sendHttpRequest(String url, String data) throws Exception { // 创建HttpClient对象 CloseableHttpClient httpClient = HttpClients.createDefault(); // 创建HttpPost对象 HttpPost httpPost = new HttpPost(url); // 设置请求头 httpPost.setHeader("Content-Type", "application/json"); // 设置请求体 StringEntity entity = new StringEntity(data, "utf-8"); httpPost.setEntity(entity); // 发送请求 CloseableHttpResponse response = httpClient.execute(httpPost); // 处理响应结果 String result = EntityUtils.toString(response.getEntity(), "utf-8"); // 关闭资源 response.close(); httpClient.close(); return CompletableFuture.completedFuture(result); } } ``` 在上述代码中,我们使用@Async注解创建了一个异步任务sendHttpRequest(),该方法用于发送HTTP请求并返回响应结果。 3. 调用异步任务:在需要异步发送HTTP请求的地方,调用异步任务即可,如下所示: ``` @Autowired private AsyncService asyncService; public void sendHttpRequests() throws Exception { List<String> urls = Arrays.asList("http://example.com/api/1", "http://example.com/api/2", "http://example.com/api/3"); List<String> data = Arrays.asList("{\"name\":\"张三\"}", "{\"name\":\"李四\"}", "{\"name\":\"王五\"}"); List<CompletableFuture<String>> futures = new ArrayList<>(); for (int i = 0; i < urls.size(); i++) { CompletableFuture<String> future = asyncService.sendHttpRequest(urls.get(i), data.get(i)); futures.add(future); } CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])).join(); List<String> results = futures.stream().map(CompletableFuture::join).collect(Collectors.toList()); // 处理响应结果 // ... } ``` 在上述代码中,我们创建了一个List<CompletableFuture<String>>类型的futures,用于存储异步任务的返回结果。然后,我们循环遍历urls和data,调用异步任务sendHttpRequest()并将返回结果添加到futures列表中。接着,我们使用CompletableFuture.allOf()方法等待所有异步任务执行完毕,然后将所有异步任务的返回结果存储在results列表中,最后对响应结果进行处理。 通过以上步骤,我们就可以实现异步批量发送HTTP请求数据至服务端。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值