码迷,mamicode.com
首页 > 编程语言 > 详细

用java进行测试php写的接口

时间:2019-10-16 11:10:58      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:exce   pack   methods   response   origin   buffere   word   pat   while   

 

<?php
/* 
* @Author: anchen
* @Date: 2018-07-06 13:53:19
* @Last Modified by: anchen
* @Last Modified time: 2018-07-06 19:22:44
*/
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Origin, X-Requested-With,Content-Type,Accept");
header("Access-Control-Allow-Methods: GET, POST , PUT,DELETE");
$Json = array(‘User_Id‘ => "1", ‘User_Name‘ =>"LiMing", ‘User_Department‘ => "RuiJie", ‘Password‘ => "88888");

echo json_encode($Json,JSON_UNESCAPED_UNICODE);

 


以上是php代码,主要封装成为json数据,header中的东西是解决跨域调用。然后在appach(2.4.9版本--wamp2.5集成环境)配置文件中配置  Ruquire all granted 和#Require local,就是配置成局域网可以访问。

package test;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URI;
import java.net.URL;

import sun.net.www.protocol.http.HttpURLConnection;




public class TestJava {


public static void main(String[] args) {
boolean result=false;
try {
String url ="http://localhost:80/work1/test.php";

String json= TestJava.getHttpResponse(url);
System.out.println(json);

} catch (Exception e) {
e.printStackTrace();
}
/*String url ="http://localhost:80/work1/test.php";
try {
String jsonstr=TestJava.getJsonString(url);
System.out.print(jsonstr);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
}
public static String getHttpResponse(String allConfigUrl) {
BufferedReader in = null;
StringBuffer result = null;
try {

URI uri = new URI(allConfigUrl);
URL url = uri.toURL();
System.out.println(url);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Charset", "utf-8");

connection.connect();

result = new StringBuffer();
//读取URL的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}

return result.toString();

} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}

return null;

}
public static String getJsonString(String urlPath) throws Exception {
URL url = new URL(urlPath);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream inputStream = connection.getInputStream();
// 对应的字符编码转换
Reader reader = new InputStreamReader(inputStream, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(reader);
String str = null;
StringBuffer sb = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
sb.append(str);
}
reader.close();
connection.disconnect();
return sb.toString();
} 
}

 

用java进行测试php写的接口

标签:exce   pack   methods   response   origin   buffere   word   pat   while   

原文地址:https://www.cnblogs.com/baobeiqi-e/p/11684175.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!