<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Apache HttpClient GET 和 POST 示例 > 原文: [https://howtodoinjava.com/resteasy/jax-rs-restful-client-using-apache-httpclient/](https://howtodoinjava.com/resteasy/jax-rs-restful-client-using-apache-httpclient/) 我們已經學習了有關構建 [RESTful Web 服務](//howtodoinjava.com/restful-web-service/ "restful-web-service")的知識。 現在學習構建 *JAX-RS REST 客戶端*以便使用 HttpClient RESTful 客戶端來使用 Web 服務。 我將重用為 [jaxrs xml 示例](//howtodoinjava.com/resteasy/resteasy-jaxb-xml-example/ "RESTEasy + JAXB xml example")編寫的代碼。 我將訪問的 HTTP GET 和 POST REST API 已定義。 ```java @GET @Path("/users/{id}") public User getUserById (@PathParam("id") Integer id) { User user = new User(); user.setId(id); user.setFirstName("demo"); user.setLastName("user"); return user; } @POST @Path("/users") public User addUser() { //Some code } ``` 要使用 [**Apache httpclient**](https://hc.apache.org/httpclient-3.x/ "apache http client") 構建 RESTful 客戶端,請遵循以下說明。 ## 1\. Apache HttpClient Maven 依賴項 ```java <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.1.1</version> </dependency> ``` ## 2\. Apache HttpClient GET API 示例 Java 程序,用于如何使用 http get 請求發送 json 數據。 ```java public static void demoGetRESTAPI() throws Exception { DefaultHttpClient httpClient = new DefaultHttpClient(); try { //Define a HttpGet request; You can choose between HttpPost, HttpDelete or HttpPut also. //Choice depends on type of method you will be invoking. HttpGet getRequest = new HttpGet("http://localhost:8080/RESTfulDemoApplication/user-management/users/10"); //Set the API media type in http accept header getRequest.addHeader("accept", "application/xml"); //Send the request; It will immediately return the response in HttpResponse object HttpResponse response = httpClient.execute(getRequest); //verify the valid error code first int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != 200) { throw new RuntimeException("Failed with HTTP error code : " + statusCode); } //Now pull back the response object HttpEntity httpEntity = response.getEntity(); String apiOutput = EntityUtils.toString(httpEntity); //Lets see what we got from API System.out.println(apiOutput); //<user id="10"><firstName>demo</firstName><lastName>user</lastName></user> //In realtime programming, you will need to convert this http response to some java object to re-use it. //Lets see how to jaxb unmarshal the api response content JAXBContext jaxbContext = JAXBContext.newInstance(User.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); User user = (User) jaxbUnmarshaller.unmarshal(new StringReader(apiOutput)); //Verify the populated object System.out.println(user.getId()); System.out.println(user.getFirstName()); System.out.println(user.getLastName()); } finally { //Important: Close the connect httpClient.getConnectionManager().shutdown(); } } ``` ## 3\. 帶有 json 正文的 Apache HttpClient POST API 示例 Java 程序,用于如何使用 http post 請求將 json 數據發送到服務器。 ```java public static void demoPostRESTAPI() throws Exception { DefaultHttpClient httpClient = new DefaultHttpClient(); User user = new User(); user.setId(100); user.setFirstName("Lokesh"); user.setLastName("Gupta"); StringWriter writer = new StringWriter(); JAXBContext jaxbContext = JAXBContext.newInstance(User.class); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.marshal(user, writer); try { //Define a postRequest request HttpPost postRequest = new HttpPost("http://localhost:8080/RESTfulDemoApplication/user-management/users"); //Set the API media type in http content-type header postRequest.addHeader("content-type", "application/xml"); //Set the request post body StringEntity userEntity = new StringEntity(writer.getBuffer().toString()); postRequest.setEntity(userEntity); //Send the request; It will immediately return the response in HttpResponse object if any HttpResponse response = httpClient.execute(postRequest); //verify the valid error code first int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != 201) { throw new RuntimeException("Failed with HTTP error code : " + statusCode); } } finally { //Important: Close the connect httpClient.getConnectionManager().shutdown(); } } ``` [源碼下載](https://docs.google.com/file/d/0B7yo2HclmjI4SF9nbVlmYUVERUk/edit?usp=sharing) 將您對 http GET 和 POST 請求的 httpclient 示例的評論發給我。 學習愉快!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看