通过HTTP请求调用restful接口

package test.demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class restfulClientPost  {

     public static void main(String[] args) {

            try {
                   //获取页面的请求的路径
                   URL targetUrl = new URL("http://localhost:8080/demo/urule/rest/test_demo/test111");
                   //打开连接
                   HttpURLConnection httpConnection = (HttpURLConnection) targetUrl.openConnection();
                   httpConnection.setDoOutput(true);
                   httpConnection.setRequestMethod("POST"); //httpConnection.setRequestMethod("GET");
                   httpConnection.setRequestProperty("Content-Type", "application/json");
                   //String input = "{\"age\"19,\"gender\":\"男\",\"name\":\"测试\",\"car\":\"TSL\"}";
                   
                  /* Json格式的字符串
                   *  "input": [
                           {
                               "name": "客户",
                               "fields": [
                                   {
                                       "name": "age",
                                       "label": "年龄"
                                   },
                                   {
                                       "name": "gender",
                                       "label": "性别"
                                   },
                                   {
                                       "name": "car",
                                       "label": "汽车"
                                   },
                                   {
                                       "name": "name",
                                       "label": "姓名"
                                   }
                               ]
                           }
                       ]*/
                   String input = "[{\"name\":\"客户\",\"fields\":{\"age\":179,\"gender\":\"男\",\"name\":\"测试\",\"car\":\"tsl\"}}]";
                   OutputStream outputStream = httpConnection.getOutputStream();
                   outputStream.write(input.getBytes());
                   outputStream.flush();

                   if (httpConnection.getResponseCode() != 200) {
                          throw new RuntimeException("Failed : HTTP error code : "
                                 + httpConnection.getResponseCode());
                   }

                   BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
                                 (httpConnection.getInputStream())));

                   String output;
                   System.out.println("Output from Server:\n");
                   while ((output = responseBuffer.readLine()) != null) {
                          System.out.println(output);
                   }

                   httpConnection.disconnect();

              } catch (MalformedURLException e) {

                   e.printStackTrace();

              } catch (IOException e) {

                   e.printStackTrace();

             }

            }     
}
 

你可能感兴趣的:(通过HTTP请求调用restful接口)