Interface Test (java+testng+ant+jenkins) First Java

Source: Internet
Author: User
Tags finally block testng

This tutorial is for people with basic Java language skills to learn

1, preparation tools: JDK, Eclipse (specific installation please Baidu)

2. In eclipse, create the project as follows

  

3, set the project code UTF-8

Project name right-click--properties--resource--other:utf-8

4, then create a class:httprequest, put in the code

(The interface can be called without scrutiny implementation)

Package Com.lx;import java.io.bufferedreader;import java.io.ioexception;import java.io.InputStreamReader; Import Java.io.printwriter;import java.net.url;import java.net.urlconnection;import Java.text.SimpleDateFormat; Import Java.util.date;import java.util.list;import java.util.Map; Public classHttpRequest { Public Static voidMain (string[] args) {String s= Sendget ("Https://translate.google.cn/translate_a/single","CLIENT=T&SL=EN&TL=ZH-CN&HL=ZH-CN&DT=AT&DT=BD&DT=EX&DT=LD&DT=MD&DT=QCA &dt=rw&dt=rm&dt=ss&dt=t&ie=utf-8&oe=utf-8&otf=1&ssel=0&tsel=0&kc=1 &tk=640089.1025185&q=and"); System. out. println (s); }            /** * a request to send a GET method to a specified URL * * @param URL * Send the requested URL * @param param * Request parameter, request     The parameters should be in the form of name1=value1&name2=value2. * @return The response result of the remote resource represented by the URL*/     Public Staticstring sendget (string url, string param) {string result=""; BufferedReaderinch=NULL; Try{String urlnamestring= URL +"?"+param; URL Realurl=NewURL (urlnamestring); //opening and linking between URLsURLConnection connection =realurl.openconnection (); //to set common request propertiesConnection.setrequestproperty ("Accept","*/*"); Connection.setrequestproperty ("Connection","keep-alive"); Connection.setrequestproperty ("user-agent",                    "mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); //establish an actual connectionConnection.connect (); //Get all response header fieldsmap<string, list<string>> map =Connection.getheaderfields (); //iterate through all the response header fields             for(String key:map.keySet ()) {System. out. println (Key +"--->"+ map.Get(key)); }            //defines the response of the BufferedReader input stream to read the URL            inch=NewBufferedReader (NewInputStreamReader (Connection.getinputstream ()));            Connection.getdate ();            String Line;  while(line =inch. ReadLine ())! =NULL) {result+=Line ; }        } Catch(Exception e) {System. out. println ("send GET request exception! "+e);        E.printstacktrace (); }        //Use the finally block to close the input stream        finally {            Try {                if(inch!=NULL) {                    inch. Close (); }            } Catch(Exception E2) {e2.printstacktrace (); }        }        returnresult; }    /** * a request to send a POST method to the specified URL * * @param URL * Send the requested URL * @param param * Request for reference     Number, the request parameter should be in the form of name1=value1&name2=value2. * @return The response result of the remote resource represented*/     Public Staticstring sendpost (string url, string param) {PrintWriter out=NULL; BufferedReaderinch=NULL; String result=""; Try{URL Realurl=Newurl (URL); //opening and linking between URLsURLConnection conn =realurl.openconnection (); //to set common request propertiesConn.setrequestproperty ("Accept","*/*"); Conn.setrequestproperty ("Connection","keep-alive"); Conn.setrequestproperty ("user-agent",                    "mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); //to send a POST request, you must set the following two linesConn.setdooutput (true); Conn.setdoinput (true); //gets the output stream corresponding to the URLConnection object             out=NewPrintWriter (Conn.getoutputstream ()); //Send Request Parameters             out. Print (param); //buffer for flush output stream             out. Flush (); //defines the response of the BufferedReader input stream to read the URL            inch=NewBufferedReader (NewInputStreamReader (Conn.getinputstream ()));            String Line;  while(line =inch. ReadLine ())! =NULL) {result+=Line ; } System. out. println (Conn.getdate ()); LongTime =conn.getdate (); Date D=NewDate (time); String Date=NewSimpleDateFormat ("YYYYMMDDHHMMSS"). Format (d); System. out. println (date); } Catch(Exception e) {System. out. println ("An exception occurred while sending the POST request! "+e);        E.printstacktrace (); }        //Use the finally block to close the output stream, input stream        finally{            Try{                if( out!=NULL){                     out. Close (); }                if(inch!=NULL){                    inch. Close (); }            }            Catch(IOException ex) {ex.printstacktrace (); }        }        returnresult; }    }

5. In Demo1, call the interface that sent the GET request

  

Request parameters from Google Translate

In this interface, you can view the interface data by pressing F12, entering and.

The picture in the red box, the question mark before the URL, the question mark after the request data.

 PackageCOM.LX;Importcom.lx.HttpRequest; Public classDemo1 { Public Static voidMain (string[] args) {//The first parameter is the URL, the second parameter is the request dataString s = httprequest.sendget ("Https://translate.google.cn/translate_a/single", "CLIENT=T&AMP;SL=EN&AMP;TL=ZH-CN &hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss& Dt=t&ie=utf-8&oe=utf-8&otf=1&ssel=0&tsel=0&kc=1&tk=640089.1025185&q=and ");    System.out.println (s); }}

6, the operation result is

Consistent with the data in the Google translation response, the test passed

Interface Test (java+testng+ant+jenkins) First Java

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.