jersey rest client

package com.neusoft.monitor.service.base;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

import org.hlj.framework.core.page.Page;

public class RestClient {

public final static String METHOD_GET = "GET";

public final static String METHOD_PUT = "PUT";

public final static String METHOD_DELETE = "DELETE";

public final static String METHOD_POST = "POST";

public static Page rest(String serviceUrl, String parameter, String restMethod) {
try {

System.setProperty("javax.net.ssl.keyStore", "D:\\zhou.tie\\.keystore");
System.setProperty("javax.net.ssl.keyStorePassword", "neusoft");
System.setProperty("javax.net.ssl.trustStore", "D:\\zhou.tie\\.keystore");
System.setProperty("javax.net.ssl.trustStorePassword", "neusoft");
String urlResouce = "https://10.10.55.171:8443/pwvm/rest/Terminal/verifyItself/T000001/10.10.55.113/8020"; // create URL

HttpsURLConnection con = (HttpsURLConnection) (new URL(urlResouce)).openConnection();
con.setRequestProperty("Charset", "UTF-8");
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestMethod("GET");
con.setRequestMethod(restMethod);
// 如果请求方法为PUT,POST和DELETE设置DoOutput为真
if (!RestClient.METHOD_GET.equals(restMethod)) {
con.setDoOutput(true);
if (!RestClient.METHOD_DELETE.equals(restMethod)) { // 请求方法为PUT或POST时执行
OutputStream os = con.getOutputStream();
os.write(parameter.getBytes("UTF-8"));
os.close();
}
} else { // 请求方法为GET时执行
InputStream in = con.getInputStream();
byte[] b = new byte[1024];
int result = in.read(b);
while (result != -1) {
System.out.write(b, 0, result);
result = in.read(b);
}
}
System.out.println(con.getResponseCode() + ":" + con.getResponseMessage());
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}

public static void main(String args[]) {
// GET
rest("https://10.10.55.171:8443/xxxx/rest/Terminal/verifyItself/T01/10.10.55.113/8020", null, RestClient.METHOD_GET);

/*//PUT
String put="<?xml version=\"1.0\" encoding=\"UTF-8\" ?><PRODUCT xmlns:xlink=\"http://www.w3.org/1999/xlink\"><NAME>Chair Shoe</NAME>"
+"<PRICE>24.8</PRICE></PRODUCT>";
rest("http://localhost:8081/sqlrest/PRODUCT/395",put,RestClient.METHOD_PUT);

//POST
String post="<?xml version=\"1.0\" encoding=\"UTF-8\" ?><PRODUCT xmlns:xlink=\"http://www.w3.org/1999/xlink\">"
+"<PRICE>98</PRICE></PRODUCT>";
rest("http://localhost:8081/sqlrest/PRODUCT/395",post,RestClient.METHOD_POST);

//DELETE
rest("http://localhost:8081/sqlrest/PRODUCT/395",null,RestClient.METHOD_DELETE);*/

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值