// 发送POST请求
public static void getPort2(){
try {
String postData="{name,age,[student1,aa1,aa2],[student2,aa1,aa2]}";
URL url = new URL("http://127.0.0.1:9080/api/interface/getConfig.ac");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","text/html");
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
out.write(postData);
out.flush();
out.close();
// 获取响应状态
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
System.out.println("connect failed!");
}
// 获取响应内容体
String line, result = "";
BufferedReader in = new BufferedReader(new InputStreamReader(
conn.getInputStream(), "utf-8"));
while ((line = in.readLine()) != null) {
result += line + "\n";
}
System.out.println("```````"+result);
in.close();
conn.disconnect();
} catch (IOException e) {
e.printStackTrace(System.out);
}
}
接收POST传过来的json数据,在struts中的Action方法中,调用request.getInputStream() 来读取数据。
try {
BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
System.out.println("portDate==="+br.readLine());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
如无法读取,注意
conn.setRequestProperty("Content-Type","text/html");
=====================================================
直接get获取数据
public static void getPort1(){
StringBuffer json = new StringBuffer();
try {
//实例一个url和URLConnection
URL oracle = new URL("http://127.0.0.1:9080/api/interface/getConfig.ac");
//打开链接
URLConnection yc = oracle.openConnection();
//输入流作参数传进InputStreamReader并用BufferedReader接受
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
String inputLine = null;
//一直读到空,并设置流编码是UTF8
while ( (inputLine = in.readLine()) != null) {
System.out.println("=="+inputLine);
json.append(new String(inputLine.getBytes(),"GBK"));
}
//记得关闭连接
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
post另外一种方式
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public static void getPort3() {
try {
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(
"http://127.0.0.1:9080/SafeSysApi/SecureServer/reportInfo.ac");
String postData = "{\"use\": \"user1\",\"account\": \"account1\",\"UserInfo\": [{\"name\": \"xwang\",\"age\": [\"aa\",\"dd\",\"aa\"]}],\"shool\": \"eee\",\"Type\": \"china\"}";
((PostMethod) method).addParameter("UserInfos", postData);
HttpMethodParams param = method.getParams();
param.setContentCharset("UTF-8");
System.out.println(param);
client.executeMethod(method);
// 打印服务器返回的状态
System.out.println(method.getStatusLine());
// 打印返回的信息
System.out.println();
InputStream stream = method.getResponseBodyAsStream();
BufferedReader br = new BufferedReader(new InputStreamReader(
stream, "UTF-8"));
StringBuffer buf = new StringBuffer();
String line;
while (null != (line = br.readLine())) {
buf.append(line).append("\n");
}
System.out.println(buf.toString());
// 释放连接
method.releaseConnection();
} catch (Exception e) {
// TODO: handle exception
}
}
对应包下载:
http://download.csdn.net/detail/wyyother1/9818669
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//接收
jsonData = getRequestParm("UserInfos");