Java 写一个供别人访问的http接口
1.引入jar包
2.读取发送过来的请求参数,判断参数是否正确
3.根据参数,查询所需要的信息
4.返回给请求端信息
例子:
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.struts2.ServletActionContext;
import cn.com.speed.busdevelop.action.account.AccountAction;
import cn.com.speed.busdevelop.po.Account;
import cn.com.speed.busdevelop.service.AccountService;
import cn.com.speed.busdevelop.service.impl.AccountServiceImpl;
import com.sdicons.json.mapper.JSONMapper;
import com.sdicons.json.mapper.MapperException;
/*
* pos管理系统接口
* 查询省经理下的所有拓展人员
*/
public String queryListCount() throws Exception{
log.info("查询省经理下的所有拓展人员");
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=UTF-8");
BufferedReader reader = new BufferedReader(new InputStreamReader(
request.getInputStream()));
StringBuffer strBuf = new StringBuffer("");
String lines;
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
strBuf.append(lines);
}
Map map = new HashMap();
// //协议参数
// String cid = request.getParameter("cid");
// String cmd = request.getParameter("cmd");
// String version = request.getParameter("version");
// String returnType = request.getParameter("returnType");
// //业务参数
// String userId = request.getParameter("userId");
//协议参数
String cid = JSONObject.fromObject(strBuf).getString("cid");
String cmd = JSONObject.fromObject(strBuf).getString("cmd");
String version = JSONObject.fromObject(strBuf).getString("version");
String returnType = JSONObject.fromObject(strBuf).getString("returnType");
//业务参数
String userId = JSONObject.fromObject(strBuf).getString("userId");
String returnVerify = JSONObject.fromObject(strBuf).getString("verify");
map.put("cid", cid);//系统标识
map.put("cmd", cmd);//命令字
map.put("version",version);//接口版本
map.put("returnType", returnType);//报文格式
// map.put("userId", userId);
String param = null;
try {
param = JSONMapper.toJSON(map).render(false);
} catch (MapperException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
log.info("参数:"+param);
// String returnVerify = request.getParameter("verify");
String key = "123456789";
InterfaceCheck check = new InterfaceCheck();
String verify = check.checkPos(userId,key);
Map resultMap = new HashMap();
String jsonStr = null;
Writer writer = response.getWriter();
if(!verify.equals(returnVerify)){
resultMap.put("result", null);
resultMap.put("resultCode", "1");//失败
resultMap.put("resultDesc","协议参数不正确,请求失败!");//结果描述
try {
jsonStr = JSONMapper.toJSON(resultMap).render(false);
} catch (MapperException e) {
e.printStackTrace();
}
log.info("返回的参数"+jsonStr);
writer.write(jsonStr);
return "";
}
System.out.println("==============================");
System.out.println(userId);
System.out.println("==============================");
Map<String, Object> retMap = new HashMap<String, Object>();
String errorMsg = "";
List<Account> list = null;
try {
AccountService service = new AccountServiceImpl();
list = service.queryListAccount(userId);
if(list!=null){
for (int i = 0; i < list.size(); i++) {
Account account = list.get(i);
retMap.put("userId", account.getUserId());
retMap.put("userName", account.getUserName());
}
}
} catch (Exception e1) {
errorMsg = errorMsg + e1.getMessage();
}
resultMap.put("result", retMap);
resultMap.put("resultCode", "0");
resultMap.put("resultDesc", "拓展人员列表查询成功!");
//转换成json
try {
jsonStr = JSONMapper.toJSON(resultMap).render(false);
} catch (MapperException e) {
e.printStackTrace();
}
try {
log.info("返回的参数"+jsonStr);
writer.write(jsonStr);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return "";
}
浙公网安备 33010602011771号