How can I send an http request at a specific time?

How do I send 1000 different requests to a server at the same time?

  • The login page has three fields,namely user number field, date of birth (dob) field and a verify question (which is nothing but some mathematical arithmetic expression). As of now the code I have written is a java class, that send http request to the website and get the result successfully back.(What I do is: I do some scraping of webpage to find out the mathematical expression, then do  post.setEntity(new UrlEncodedFormEntity(postParams));   where the postParams has the user number and date of birth and the result of the mathematical expression). Now what I would like to do is, create 1000 request at the same time, each with different unique date of birth and send them to server and get the result back. If the result is found I want to stop rest 999 request and move on. I did some research on stackoverflow and before spending my efforts and I want to do the efficient way. Following are the points that I am struck now 1) If I am doing multi threading should I do it in the java code or should I make use of http client?  (Currently I am using httpClient of Apache) 2) I am mainly struck with the logic of writing 1000 request each with unique date of birth and sending those 1000 request at one shot. (Creating 1000 requests with unique dob and sending them to server should happen in less than half a second). The main problem is 1000 request are not same, they have one parameter which is unique in all 1000 requests). 3) I don't want my 1000 requests to get queued or waiting and my program to crash because of threading. ( I am using 16 gb ram, i7 quad core processor) PS:  What I am actually trying to do is brute force way of logging in to a website. I made sure this is legal. I have double confirmed this. So there are two fields in the sign in page. One is the username which I know and the other is a number between 1 and 1000. So for each user (A, B, C...) I should make 1000 attempt. User A with number 1 as 1st login attempt User A with number 2 as 2nd login attempt User A with number 3 as 3rd login attempt User A with number 4 as 4th login attempt. Since the number of users are around 2 million, I want to make the attempt less than a second for one user(in my example it would be user A). So I thought of creating 1000 attempts at one shot for user A and sending to the website. Though I don't know how many requests my target website can handle, I would like to assign each number 1,2,3...1000 for user A using multithreading (or any other better technique which you might have known about) and sending the requests to the target site. I don't want them to be queued as my aim is to get the response as soon as possible.

  • Answer:

    First, it sounds like you are intending to break into a system via password guessing.  Any properly designed login system won't allow password guessing attacks.  Login systems should include anti-forgery tokens, throttling, spam detection, and CAPTCHAs all to prevent just this type of password guessing. Another hurdle, I don't think it's possible to do this in the Java VM, at least not without writing your own TCP code. Certain limits and throttles are enforced in the JVM implementation on how many HTTP connections you can open at a time to a single host. Also, servers often do their own throttling of number of connections, in order to prevent DoS attacks, so if the client doesn't throttle, the server might start dropping connections on it's own. There's an extension of the HTTP protocol called pipelining, which allows you to send HTTP requests asynchronously.  However, that is also throttled on the client and server side. If you control both the client and the server, there's many more options. You can just ignore HTTP and design your own custom asynchronous protocol.

Jeff Nelson at Quora Visit the source

Was this solution helpful to you?

Other answers

You evidently want to do a Load Test and measure performance under certain conditions. Instead of coding everything by hand, I would recommend using something like JMeter http://jmeter.apache.org/ Like any other decent load testing tool, it allows you to record your login scenario test (instead of coding it by hand), and then running it on one or more servers depending on your needs. You can tweak all kinds of things and get reports. You will save dozens of hours.

Faris Zacina

For each Login request, you can spawn a thread and in case a Thread finds a match interrupt all others threads with Thread.interrupt() [http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html] OR you can force stop them with Thread.stop() [though it is strictly not advised to do so]. The worker threads that you spawn will use Apache HTTP components to do the business logic if I got your question right. Also consider the possibility of using AJAX requests to your business class which has the logic to find a match. This way users can work seamlessly with your application while the back-end jobs are running. But I need some clarity on this. Are the requests based on user-input or do you randomly generate them? UPDATE Pseudocode for the given use-case : Here's what I could gather : 1. User inputs his username 2. You call the sendPost() method which takes two parameters : URL and username 3. Loop through numbers 1 to 1000 and each time inside the loop :    - Send a POST request to the URL with two postParams : username and the current loop counter    - If responseCode equals 200, then break out of the loop. This is a synchronous approach which can be time consuming. Since these POST requests are independent operations we can use multi-threading(parallel processing) : 1.  User inputs his username 2. Create an object(say, lock) of type CyclicBarrier and to its constructor pass the number parties to be 1001(1000 of our worker threads + 1 main() thread) [http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CyclicBarrier.html] . 3. In the run() method of each worker thread that has the time consuming logic(calls sendPost()), before doing any operation put this line at the first :   lock.await();    Also when responseStatus equals 200(ins sendPost()) like I mentioned earlier, you can interrupt or force stop all other worker threads. 4. Loop for 1000 times and each time inside the loop, spawn a worker thread passing username to the constructor and call threadInstance.start(). 5. None of the threads would start their operation because the lock is released only when all the parties(=1001) invoke lock.await(). 6. Now after the loop in the main() method(which is the 1001th thread) invoke lock.await() . This one statement would now start all our worker threads at once. See if this works for you.

Radhikaa Bhaskaran

Regarding point 4, the number of requests an apache server can handle will vary widely depending on a number of factors. What kind of hardware is it? Is it a single server or many servers load balanced? Presumably there is a custom web application talking to a database of some sort, so there are also things there that could cause delays. Apache default configs generally allow about 150 connections. If you're looking at a single apache server, I would say you are almost guaranteed not to have it be able to service 1000 simultaneous requests. I would caution you to make sure you have permission to do this. That many requests at once could render the service unusable.

Blake Swopes

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.