http形式的webservice

时间:2023-03-09 02:32:32
http形式的webservice
 import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map; import com.google.gson.Gson;
/**
* http格式的webservice主要工具类
* @author Administrator
*
*/
public class HttpWebServiceUtils { /*
* params 填写的URL的参数 encode 字节编码
*/
public static String sendPostMessage(Map<String, String> params,String encode,String path) {
URL url = null;
try {
url = new URL(path);
} catch (MalformedURLException e1) {
e1.printStackTrace();
return "";
}; StringBuffer stringBuffer = new StringBuffer(); if (params != null && !params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {
try {
stringBuffer
.append(entry.getKey())
.append("=")
.append(URLEncoder.encode(entry.getValue(), encode))
.append("&");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return "";
}
}
//删掉最后一个 & 字符
stringBuffer.deleteCharAt(stringBuffer.length() - 1);
System.out.println("传递的所有参数信息:" + stringBuffer.toString());
HttpURLConnection httpURLConnection = null;
try {
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setConnectTimeout(3000);
httpURLConnection.setDoInput(true);// 从服务器获取数据
httpURLConnection.setDoOutput(true);// 向服务器写入数据 //获得上传信息的字节大小及长度
byte[] mydata = stringBuffer.toString().getBytes();
//设置请求体的类型
httpURLConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
httpURLConnection.setRequestProperty("Content-Lenth",String.valueOf(mydata.length)); //获得输出流,向服务器输出数据
OutputStream outputStream = (OutputStream) httpURLConnection.getOutputStream();
outputStream.write(mydata);
outputStream.flush();// 强制把缓冲区的数据写入到文件并清空缓冲区
outputStream.close();// 关闭连接
//获得服务器响应的结果和状态码
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == 200) {
//获得输入流,从服务器端获得数据
InputStream inputStream = (InputStream) httpURLConnection.getInputStream();
return (changeInputStream(inputStream, encode));
}
} catch (IOException e) {
e.printStackTrace();
return "";
} finally {
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
}
}
return "";
} /*
*把从输入流InputStream按指定编码格式encode变成字符串String
*/
public static String changeInputStream(InputStream inputStream,String encode) { // ByteArrayOutputStream 一般叫做内存流
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len = 0;
String result = "";
if (inputStream != null) { try {
while ((len = inputStream.read(data)) != -1) {
byteArrayOutputStream.write(data, 0, len);
}
result = new String(byteArrayOutputStream.toByteArray(), encode); } catch (IOException e) {
e.printStackTrace();
}
}
return result;
} public static void main(String[] args) {
//http://localhost:8088/online.video/account/account!httpAddAccount.do
//?account.userId=123&account.password=2&account.name=2&account.sex=1&account.type=0&account.phone=2
//params是请求的参数的map形式
Map<String, String> params = new HashMap<String, String>();
params.put("account.userId", "1234");
params.put("account.password", "123");
params.put("account.name", "张三");
params.put("account.sex", "0");
params.put("account.type", "0");
params.put("account.phone", "13888888888");
//发送请求,得到响应结果
String result = HttpWebServiceUtils.sendPostMessage(params, "UTF-8",
"http://localhost:8088/online.video/account/account!httpAddAccount.do");
System.out.println(result);
Gson gson = new Gson(); /*
* 下面是对json格式的结果做json解析,用的是gson
*/ /*GsonResult gsonResult = gson.fromJson(result, GsonResult.class);
System.out.println(gsonResult.getSuccess());*/
}
}

仿main方法的调用就可以了,接口方法就是在Controller里写一个这个请求的方法