Java中多线程和httpclient的正确做法是什么?

时间:2015-10-27 19:20:05

标签: java multithreading apache-httpclient-4.x

我正在创建一个可以执行多次登录的程序。我还将为每个登录提供将项目添加到购物车和购买的功能。该代码目前正在为一个帐户工作,这是非常基本的。我不得不删除一些私人信息,但代码仍然应该清楚。再说一遍,我只是想知道我应该采取什么方法进行多次登录?这段代码在大多数情况下看起来最适合速度吗?如果结帐返回500,我如何处理重试尝试?此代码目前仅针对单次登录进行设置。此外,我发现没有多少文章可以正确清理HttpClient。至少,我不认为我发现的教程非常有信誉。

再次感谢您花时间阅读本文,我只是想学习其他实践来改进我的代码并采用适当的多线程技术。

关于我的代码的更多详细信息,当您查看页面时会检索到令牌,令牌会在整个程序中存储和使用。

    class example {

        private static List<Header> headers = new ArrayList<Header>();
        private static BasicCookieStore cookieStore = new BasicCookieStore();
        private static CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultHeaders(headers).setDefaultCookieStore(cookieStore).build();

        public static void main(String[] args) throws Exception {
        String loginURL = "...";
        String productUrl = "...";
        String userid = "";
        String password = "";
        String formKey = "";
        int size = 9;
        Boolean debug = true;
        JsonElement product;

        headers.add(new BasicHeader("Host", "..."));
        headers.add(new BasicHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"));
        headers.add(new BasicHeader("Accept-Language", "en-us"));
        headers.add(new BasicHeader("Content-Encoding", "gzip, deflate"));
        headers.add(new BasicHeader("Content-Type", "Application/x-www-form-urlencoded"));
        headers.add(new BasicHeader("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 8_4_1 like Mac OS X) "
                    + "AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12H321 Safari/600.1.4"));
        headers.add(new BasicHeader("Connection", "keep-alive"));

        Scanner in = new Scanner(System.in);
        Gson gson = new Gson();

        System.out.println("Select Profile\n1. ...\n2. Custom");

        int select = in.nextInt();

        switch(select) {

        case 1:
            userid = "...";
            password = "...";
            break;
        // Custom Account Login
        case 2:
            System.out.println("Enter User_ID:");
            userid = in.next();
            System.out.println("Enter Password:");
            password = in.next();
            break;
        }

        int input = 0;
        do {
            input = in.nextInt();
            switch(input) {
                case 99:
                    debug = true;
                break;
                // View Menu.
                case 0:
                    System.out.println("...");
                    break;
                // Initiate Session
                case 1: 
                    // Retrieve formKey for login page.
                    formKey = getLogin(GetPageContent(loginURL));
                    session(formKey, userid, password, debug);
                    System.out.println("Press 0 to View Menu");
                    break;
                case 2:
                    product = getProduct(GetPageContent(productUrl));

                    // Retrieve Product ID
                    Product productInfo = gson.fromJson(product.getAsJsonObject(), Product.class);

                    // Retrieve Color
                    Product[] color = gson.fromJson(product.getAsJsonObject().getAsJsonObject("attributes").getAsJsonObject("92").getAsJsonArray("options"), Product[].class);

                    // Prepare product request
                    String productKey = productInfo.getProductId();
                    String colorId = color[0].getId();
                    String postUrl = productInfo.getPostUrl();

                    // Execute addToCart
                    String result = addToCart(formKey, postUrl, productKey, colorId, size, debug);
                    break;
                case 3:
                    String result2 = checkout(formKey);
                    break;
            }
        } while(input != 0);

        }

        public static void session(String formKey, String userid, String password, Boolean debug) { 
            HttpPost post = new HttpPost("...");
            try {
                // Package the data
                StringEntity entity = new StringEntity("...");
                post.setEntity(entity);

                // Execute the data
                HttpResponse response = httpClient.execute(post);

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                post.releaseConnection();
                System.out.println("Login execution completed.");
            }
        }

        public static String addToCart(String formKey, String postUrl, String productId, String colorId, int size, Boolean debug) {
            String result = "";
            HttpPost post = new HttpPost(postUrl);
            try {
                StringEntity entity  = new StringEntity("...");
                post.setEntity(entity);

                // Execute the data
                HttpResponse response = httpClient.execute(post);

                System.out.println("Response Code : " 
                        + response.getStatusLine().getStatusCode());

                // RETURN RESULT
                result = EntityUtils.toString(response.getEntity());

                if(debug) {
                    System.out.println("LOG: " + result);
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                post.releaseConnection();
                System.out.println("Adding to cart execution completed.");
            }
            return result;
        }

        public static String GetPageContent(String url) throws Exception {
            StringBuffer result = null;
            HttpGet request = new HttpGet(url);
            try {
                HttpResponse response = httpClient.execute(request);
                int responseCode = response.getStatusLine().getStatusCode();

                System.out.println("\nSending 'GET' request to URL : " + url);
                System.out.println("Response Code : " + responseCode);

                BufferedReader rd = new BufferedReader(
                            new InputStreamReader(response.getEntity().getContent()));

                result = new StringBuffer();
                String line = "";


                while ((line = rd.readLine()) != null) {
                    result.append(line);
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally { 
                request.releaseConnection();
            }
            return result.toString();
         }

        public static String getLogin(String html) {
            String formKey = "";

            Document doc = Jsoup.parse(html);

            Element loginform = doc.getElementById("login-form");
            Elements inputElements = loginform.getElementsByTag("input");

            for (Element inputElement : inputElements) {
                String key = inputElement.attr("name");
                String value = inputElement.attr("value");

                if (key.equals("form_key"))
                    formKey = value;
            }

            return formKey;

        }

        public static JsonObject getProduct(String html)
                throws UnsupportedEncodingException {

        Document doc = Jsoup.parse(html);

        System.out.println("Extracting form's data...");

        Element form = doc.getElementById("product_addtocart_form");
        Elements formElements = form.getElementsByTag("input");

        String rawScript = form.getElementsByTag("script").html();

        String script = "{" + rawScript.substring(rawScript.lastIndexOf("g({") + 3, rawScript.indexOf("}});")) + "}}";

        // Create JSON object
        JsonElement jelement = new JsonParser().parse(script.trim());
        JsonObject jobject = jelement.getAsJsonObject();
        jobject = jobject.getAsJsonObject();

        // Retrieve post link.
        jobject.addProperty("postUrl", form.attr("action"));

        return jobject;
      }

        public static String checkout(String formKey) {
            HttpPost post = new HttpPost("...");
            String result = "";
            try {
                StringEntity entity = new StringEntity("...");
                post.setEntity(entity);

                HttpResponse response = httpClient.execute(post);
                int status = response.getStatusLine().getStatusCode();

                if(status == 200) {
                    result = EntityUtils.toString(response.getEntity());
                    System.out.println("Checkout Result: " + result);
                } else if(status == 302) {
                    System.out.println("Checkout failed, Code: 302.");
                } else if(status == 404) {
                    System.out.println("Checkout failed, Code: 404.");
                } else if(status == 500) {
(insert retry step here)
                    System.out.println("Webserver is probably down. Code, 500.");
                }

            } catch (Exception e)  {
                e.printStackTrace();
                }
            finally { 
                post.releaseConnection();
            }
            return result;
        }
    }

0 个答案:

没有答案