Simulate several methods of initiating HTTP request

Source: Internet
Author: User
Tags http post stringbuffer
How to use the HTTP protocol to invoke server-side services.
In the daily work of many times we will need to test their own methods provided, many times some methods across the platform for Third-party use, general and server communication protocols are selected HTTP protocol,
It is recommended that you study the HTTP protocol carefully. There are also a number of WebService methods available for callers to use.


Let me briefly write down the server-side service if the request to impersonate HTTP locally is invoked.
1. First simulate a server-side method, I simply wrote a test project, deployed to Tomcat above, after the success can invoke the service.
Request the Main method, write a Get and post method. Two different requests for HTTP requests, one GET request and another POST request.
@RequestMapping ("test.do") public void Test (HttpServletRequest request,httpservletresponse response) throws
	ioexception{response.getwriter (). Write ("Hello world!"); @RequestMapping ("get.do") public void Testget (HttpServletRequest request,httpservletresponse response) throws Ioexce
		ption{request.setcharacterencoding (paramsutil.utf_8);
		Response.setcontenttype ("Text/html;charset=utf-8");
		String param1 = Request.getparameter ("param1");
		String param2 = Request.getparameter ("param2");
	Response.getwriter (). Write ("param1=" +param1+ "param2=" +param2); @RequestMapping ("post.do") public void Testpost (HttpServletRequest request,httpservletresponse response) throws Ioex
		ception{request.setcharacterencoding (paramsutil.utf_8);
		Response.setcontenttype ("Text/html;charset=utf-8");
		String line = "";
		StringBuffer buffer = new StringBuffer ();
		PrintWriter out = Response.getwriter (); BufferedReader bufferreader = new BufferedReader (New InputStreamReader (Request.getinPutstream (), paramsutil.utf_8));
		while (line = Bufferreader.readline ())!= null) {buffer.append (line);
		} bufferreader.close ();
		String postdata = buffer.tostring ();
	Out.write (PostData); }



2. Start Tomcat, test whether the project is successfully deployed.

In the Explorer Bar, enter the following address: Http://localhost:8080/gateway/test.do see the following interface to show that your project deployment success ~


Figure 1 launching the project into the test interface
3. Prepare to simulate HTTP request, here mainly introduces three kinds of methods, for JMeter simulate HTTP request respectively, the Explorer bar enters URL request, Curl command realizes.



A. JMeter http request Test
1. Configure JMeter, and follow these steps to create an HTTP request. New test plan--new thread Group--New HTTP request under Thread Group--New view result tree below thread Group
Configure in Figure 2 to test the POST request first. After the configuration is complete, click the Start button, and then look at the results below the result tree after the request finishes. Figure 3.


Figure 2 Jmeter HTTP request configuration


2. Simulate POST request
Test the POST request by configuring it in Figure 2.

Click the Green Start button, you can launch an HTTP POST request to the http://127.0.0.1:8080/gateway/post.do, corresponding to the above processing method, the result returns the input post data; see Figure 3 Click to view result tree


Figure 3 POST Request response data

3. Simulate GET request

Follow Figure 4 for JMeter configuration. Two request parameters are set


Figure 4 after the GET request configuration is completed, click on the Start button to make an HTTP request according to the url:http://127.0.0.1:8080/gateway/get.do?param1=hello&param2=world below. View the result tree after the request is complete, as shown in Figure 5

Figure 5 GET Request response data
two. browser bar enter URL request

is typically used for GET request testing

Enter Http://127.0.0.1:8080/gateway/get.do?param1=hello&param2=world directly in the Explorer Bar and press ENTER, as shown in Figure 6


Figure 6 Browser Request response data

three. Curl Command Implementation

1. The General Windows Command window does not support the Curl command, so you need to download a Curl command tool before you can execute the command by entering the Curl.exe directory after the download is complete. It can be done directly on the Linux machine, and here is the case for Windows. Crul.exe in D, so go to the D-Disk directory and view the Crul command prompt.


Figure 7 Running the Curl command on Windows

2. Simulate HTTP request

Example 1:curl-d "Hello World" http://127.0.0.1:8080/gateway/post.do run results as shown in Figure 8


Figure 8 Example 1

Example 2:curl-g "Http://127.0.0.1:8080/gateway/get.do?param1=hello&param2=world"

Here you may be mainly to the URL of the request with a colon enclosed, this is why. If not enclosed, the requested parameter can only be obtained param1,param2. Of course, there are other ways to solve this problem, here is not popular, we are interested in their own can go to search AH ~ implementation results as shown in Figure 9


Figure 9 Example 2 execution results


General work often encounter the need to test an HTTP service, here provides 3 ways to draw lessons from, of course, there are other ways, these three I think is the most convenient and simple, I hope to help.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.