java開發接口利用http協議傳輸數據java
這個接口主要用來登陸,java服務器提供一個接口,移動設備客戶端(android和ios)能經過這個接口把用戶名和密碼之類的東東傳過來到服務器驗證,而後服務器返回數據告訴客戶端是否登陸成功。好比0是成功,1是失敗。中間數據的傳遞都是經過http協議完成.這個接口該怎麼寫?android
以上是百度中搜到的問題,獲得了回覆。從新整理總結一下:ios
接口執行過程是這樣的:以客戶端登陸爲例——從客戶端界面獲得用戶名,密碼。傳到後臺(解釋下此處所說的後臺,一個程序通常是MVC模式,V視圖層,C控制層),就是說從視圖層獲得數據傳輸到控制層,控制層中有以下代碼,請看1.1。再由控制層經過http協議向服務器發送數據,代碼中接收報文的地址就是服務器地址,其中「TestTransfers」 是服務器web.xml中配置的Servletweb
<servlet>服務器
<display-name>TestTransfers</display-name>app
<servlet-name>TestTransfers</servlet-name>加密
<servlet-class>com.servlet.TestTransfers</servlet-class>url
<!-- 啓動優先級 -->.net
<load-on-startup>4</load-on-startup>xml
</servlet>
<servlet-mapping>
<servlet-name>TestTransfers</servlet-name>
<url-pattern>/TestTransfers</url-pattern>
</servlet-mapping>
1.1客戶端請求程序代碼:
public void sendMessage() throws Exception {
System.out.println("調用servlet開始=================");
StringBuffer sendStr = new StringBuffer();
sendStr.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
sendStr.append("<report_data>");
sendStr.append("<request_req>953943547334</request_req>");
sendStr.append("<request_time>2012040211394324</request_time>");
sendStr.append("<request_param>");
sendStr.append("<query_month>201203</query_month>");
sendStr.append("</request_param>");
sendStr.append("</report_data>");
BufferedReader reader = null;
try {
String strMessage = "";
StringBuffer buffer = new StringBuffer();
// 接報文的地址
URL uploadServlet = new URL(
"http://localhost:9090/TestTransfers");
HttpURLConnection servletConnection = (HttpURLConnection) uploadServlet
.openConnection();
// 設置鏈接參數
servletConnection.setRequestMethod("POST");
servletConnection.setDoOutput(true);
servletConnection.setDoInput(true);
servletConnection.setAllowUserInteraction(true);
// 開啓流,寫入XML數據
OutputStream output = servletConnection.getOutputStream();
System.out.println("發送的報文:");
System.out.println(sendStr.toString());
output.write(sendStr.toString().getBytes());
output.flush();
output.close();
// 獲取返回的數據
InputStream inputStream = servletConnection.getInputStream();
reader = new BufferedReader(new InputStreamReader(inputStream));
while ((strMessage = reader.readLine()) != null) {
buffer.append(strMessage);
}
System.out.println("接收返回值:" + buffer);
} catch (java.net.ConnectException e) {
throw new Exception();
} finally {
if (reader != null) {
reader.close();
}
}
}
代碼1.2爲服務器接口,接收到客戶端的信息解析驗證,並返回須要的信息。
1.2響應程序代碼
public class TestTransfers extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
//判斷請求報文是否來自代維繫統的ip地址
String ip = request.getRemoteHost();
// 獲取收到的報文
BufferedReader reader = request.getReader();
String line = "";
StringBuffer inputString = new StringBuffer();
while ((line = reader.readLine()) != null) {
inputString.append(line);
}
//若有必要,能夠在報文中增長其餘驗證和加密的參數
//解析獲取到的報文,根據ip地址、其餘驗證、加密等等來判斷請求報文的服務器是否有權限
//若是請求驗證合格,則根據請求的參數裝配返回的報文
// 要返回的報文
StringBuffer resultBuffer = new StringBuffer();
resultBuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
resultBuffer.append("<report_data>");
resultBuffer.append("<respon_req>953947334</respon_req>");
resultBuffer.append("<respon_time>20120402113943</respon_time>");
resultBuffer.append("<result>");
resultBuffer.append("<id>0000</id>");
resultBuffer.append("<comment>成功</comment>");
resultBuffer.append("</result>");
resultBuffer.append("<items>");
resultBuffer.append("<item>");
resultBuffer.append("<county>長治縣</county>");
resultBuffer.append("<company>鐵通</company>");
resultBuffer.append("<speciality>線路</speciality>");
resultBuffer.append("<personnel>王加和</personnel>");
resultBuffer.append("<begin_time>20120301000000</begin_time>");
resultBuffer.append("<end_time>20120331235959</end_time>");
resultBuffer.append("<plan_quantity>50</plan_quantity>");
resultBuffer.append("<checkout_quantity>40</checkout_quantity>");
resultBuffer.append("<patrol_rate>0.80</patrol_rate>");
resultBuffer.append("</item>");
//......
//......
//......
//循環組裝響應的報文
resultBuffer.append("</items>");
resultBuffer.append("</report_data>");
// 設置發送報文的格式
response.setContentType("text/xml");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.println(resultBuffer.toString());
out.flush();
out.close();
}
}