Get and post for HttpClient

Source: Internet
Author: User

Pom.xml

        <dependency>            <groupId>org.apache.httpcomponents</groupId>            <artifactId> httpclient</artifactid>            <version>4.4.1</version>        </dependency>        < dependency>            <groupId>org.testng</groupId>            <artifactId>testng</artifactId>            <version>6.8.5</version>        </dependency>        <dependency>            <groupId> org.json</groupid>            <artifactId>json</artifactId>            <version>20090211</ Version>        </dependency>

Class Code
 Public classHttpClient {@Test Public voidtest_temp () {//String url= "http://med.ihbedu.com/gateway/web/home/door/findLessonPackage?_q_=1469948710642&skuid=2c91b6905493e1810154947b705a0063 ";//This .        HttpGet (URL); //Test Platform Western medicine registered usersString PostURL = "http://test.ihbedu.com:1457/gateway/web/home/registered/createUserAccount?_q_=1458452488928"; Try{String Usernameorphone= "15988880018"; Jsonobject parsers=NewJsonobject (); Parsers.put ("Phone", Usernameorphone); Parsers.put ("Password", "000000"); Parsers.put ("Picvalidatecode", "C4kx"); Parsers.put ("Remotevalidatecode", "Success"); Parsers.put ("Phonevalidatecode", "003023"); Parsers.put ("Yesido",true); Jsonarray Provincearray=NewJsonarray (); Jsonobject Provinceinfoobject=NewJsonobject (); Provinceinfoobject.put ("id", "110000"); Provinceinfoobject.put ("Municipalities",true); Provinceinfoobject.put ("Name", "Beijing"); Provinceinfoobject.put ("Regionpath", "/110000");            Provincearray.put (Provinceinfoobject); Parsers.put ("Province", Provincearray); Parsers.put ("SchoolID", "122"); Jsonarray Schoolarray=NewJsonarray (); Jsonobject Schoolinfoobject=NewJsonobject (); Schoolinfoobject.put ("AreaPath", "/110000"); Schoolinfoobject.put ("Available",true); Schoolinfoobject.put ("id", "122"); Schoolinfoobject.put ("Name", "Beijing (Other)"); Schoolinfoobject.put ("PlatformID", "2c9180e5520a5e70015214fb2849000a"); Schoolinfoobject.put ("Platformversionid", "2c9180e5520a6063015214fc062d0006"); Schoolinfoobject.put ("ProjectID", "1"); Schoolinfoobject.put ("Subprojectid", "1");            Schoolarray.put (Schoolinfoobject); Parsers.put ("School", Schoolarray); Parsers.put ("Name", Usernameorphone); Parsers.put ("Sex", "1"); Parsers.put ("Registersource", 1); Parsers.put ("Registertype", 11); Parsers.put ("AccountType", 1);  This. HttpPost (PostURL, parsers); } Catch(jsonexception e) {e.printstacktrace (); }    }     Public voidhttpget (String url) {closeablehttpclient httpClient= Httpclients.createdefault ();//Establish HttpClientHttpGet HttpGet =NewHttpGet (URL);//Establish HttpGetSYSTEM.OUT.PRINTLN ("Get Request Address:" +Httpget.geturi ()); Try{closeablehttpresponse response= Httpclient.execute (HttpGet);//perform a GET request and save the resultSYSTEM.OUT.PRINTLN ("Get Request returned status code:" +response.getstatusline (). Getstatuscode ()); Httpentity httpentity= Response.getentity ();//Convert saved response to Entities            Try {                if(Httpentity! =NULL) {{System.out.println (The response value returned by the GET request: "+entityutils.tostring (httpentity)); }                }            } finally{entityutils.consume (httpentity);//Close EntityResponse.close ();//Close Response            }        } Catch(IOException e) {e.printstacktrace (); } finally {            Try{httpclient.close ();//Close HttpClient}Catch(IOException e) {e.printstacktrace (); }        }    }     Public voidhttppost (String URL, jsonobject parser) {closeablehttpclient httpClient=Httpclients.createdefault (); HttpPost HttpPost=Newhttppost (URL); if(Parser! =NULL) {stringentity stringentity=NewStringentity (Parser.tostring (), "utf-8");//Constructing string DataStringentity.setcontentencoding ("Utf-8");//set content encoding formatStringentity.setcontenttype ("Application/json");//Set Content TypeHttppost.setentity (stringentity);//Set Request Data        }        Try{closeablehttpresponse response=Httpclient.execute (HttpPost); System.out.println ("The status code returned by the POST request:" +response.getstatusline (). Getstatuscode ()); System.out.println (The response value returned by the GET request: "+entityutils.tostring (Response.getentity ())); } Catch(IOException e) {e.printstacktrace (); } finally {            Try{httpclient.close ();//Close HttpClient}Catch(IOException e) {e.printstacktrace (); }        }    }}

Get and post for HttpClient

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.