Java REST Client

GET:

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

public class GETRestClient {

    private static final String targetURL = "http://localhost:8080/path/get.serv";

    public static void main(String[] args) {

        try {

            URL restServiceURL = new URL(targetURL);

            HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL.openConnection();
            httpConnection.setRequestMethod("GET");
            httpConnection.setRequestProperty("Accept", "application/json");

            if (httpConnection.getResponseCode() != 200) {
                throw new RuntimeException("HTTP GET Request Failed with 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();
        }

    }
}

POST:

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 POSTRestClient {
    private static final String targetURL = "http://localhost:8080/path/post.serv";

    public static void main(String[] args) {
        try {

            URL targetUrl = new URL(targetURL);

            HttpURLConnection httpConnection = (HttpURLConnection) targetUrl.openConnection();
            httpConnection.setDoOutput(true);
            httpConnection.setRequestMethod("POST");
            httpConnection.setRequestProperty("Content-Type", "application/json");

            String input = "{\"id\":1,\"no\":\"no0003\",\"name\":\"jack\"}";

            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();
        }

    }
}
时间: 2024-10-05 03:41:45

Java REST Client的相关文章

安装java memcached client到maven repository

由于目前java memcached client没有官方的maven repository可供使用,因此使用时需要手动将其安装到本地repository.java memcached client的jar包下载地址:https://github.com/gwhalin/Memcached-Java-Client/downloads 目前2.6.2版本的java memcached client要依赖slf4j-simple.slf4j-api.commons-pool三个包,依此我们可以为其编

Java E-mail Client with GUI

Download: https://github.com/sambee/JavaMailClientWithGUI Java E-mail Client with GUI

Java设置Client Socket链接Server超时时间

Java设置Client Socket链接Server超时时间 学习了:http://blog.csdn.net/tterminator/article/details/52494141 http://blog.csdn.net/fw0124/article/details/41227543 整理如下: Socket client = null; // 创建一个流套接字,连接到指定主机上的指定端口号 // client = new Socket(IP, PORT); client = new S

elasticsearch系列七:ES Java客户端-Elasticsearch Java client(ES Client 简介、Java REST Client、Java Client、Spring Data Elasticsearch)

一.ES Client 简介 1. ES是一个服务,采用C/S结构 2. 回顾 ES的架构 3. ES支持的客户端连接方式 3.1 REST API ,端口 9200 这种连接方式对应于架构图中的RESTful style API这一层,这种客户端的连接方式是RESTful风格的,使用http的方式进行连接 3.2 Transport 连接 端口 9300 这种连接方式对应于架构图中的Transport这一层,这种客户端连接方式是直接连接ES的节点,使用TCP的方式进行连接 4. ES提供了多种

Intro to Jedis – the Java Redis Client Library

转自:http://www.baeldung.com/jedis-java-redis-client-library 1. Overview This article is an introduction to Jedis, a client library in Java for Redis – the popular in-memory data structure store that can persist on disk as well. It is driven by a keyst

【Hadoop】HA 场景下访问 HDFS JAVA API Client

客户端需要指定ns名称,节点配置,ConfiguredFailoverProxyProvider等信息. 代码示例: package cn.itacst.hadoop.hdfs; import java.io.FileInputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.URI; import org.apache.hadoop.conf.Configuration; impor

Java memcache Client 数据操作源码剖析

 在学习使用Java_Memcache操作memcache后,饶有兴趣的研究了一下Java_Memcache的源码.Java_Memcache在类AscIIClient中封装了数据操作方法set/add/delete/append/get等. 存储数据set 由Memcache命令详解,我们知道memcache原始的set命令格式为 set <key> <flag> <expiretime> <bytes> \r\n <value> \r\n

java socket client

用tornado做了个socket server.无奈联调的人员对接不上. 于是撸出了以下demo import java.io.*; import java.net.*; public class SocketTest{ SocketTest(){} void test() { try{ Socket requestSocket = new Socket("xxx.xxx.xxx.xxx", 60006); OutputStream out = requestSocket.getOu

java https client信任所有证书

package httpsclient; import java.io.IOException;import java.util.List;import java.util.ArrayList; import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.ClientProtocolException;import org.apache.http.clien

Java solrj client 添加JavaEntity Bean

Java类中字段属性添加field注释例如 public class Document {    // 文献信息    @Field    private String id; // 文献的id    @Field    private String entitype;// 表名    @Field    private String authors; // 文献的作者    @Field    private String title; // 文献标题    @Field    private