在我之前的工作經歷中有使用HttpClient直接進行的網絡數據的傳輸,然後在這裏我就將這個的過程以及相關的代碼記錄一下以防之後可能會使用到。
在這裏我們簡單的記錄使用Httpclient進行接口的服務提供以及服務調用的兩個方面的過程
服務端
在使用Httpclient進行數據交互的時候服務端的要求比較簡單,一個servlet即可,在其中的部分數據的獲取可能存在某些特殊的步驟,但是總體來說還是一個簡單的servlet.
我們這就直接創建工程以及servlet(詳細過程掠過)
具體的服務類的代碼如下
package httpTest;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class service extends HttpServlet {
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("xxxxxxxxxxxxxxxxxxxxxxx");
try {
//準備存放數據的byte數組
byte[] bufSource = null;
// 獲取到輸入流
InputStream ins = request.getInputStream();
//準備緩衝讀取流
BufferedInputStream inStream = new BufferedInputStream(ins);
//準備緩衝輸出流
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int read = 0;
//每次讀取1024進行轉存
while ((read = inStream.read(b)) > 0) {
byteOut.write(b, 0, read);
}
//寫入到數組中
bufSource = byteOut.toByteArray();
//關閉流
byteOut.close();
inStream.close();
ins.close();
//流轉字符串 字符集自選
String strRequest = new String(bufSource, "GBK");
System.out.println("strRequest = "+strRequest);
try {
//獲取輸出流
ServletOutputStream out = response.getOutputStream();
response.setContentType("text/plain:charset=GBK");
response.addHeader("Content-Disposition",
"inline; filename=\"temp.xml\"");
response.reset();
out.print(" zhengfang.sun response");
out.flush();
} catch (Exception ie) {
throw ie;
} finally {
}
} catch (Exception e) {
}
}
}
然後我們將我們這個servlet在web.xml裏面註冊一下
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>httpTest.service</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/test/*</url-pattern>
</servlet-mapping>
然後我們直接啓動Tomcat就直接能夠看到我們的結果了
客戶端
我們有了服務端關鍵還是需要能夠在其他應用中調用到這個服務纔行,在服務端的例子中我們就簡單的使用一個java工程來進行網絡接口的調用,方便我們能夠直觀的看到我們的結果
新建一個java工程,然後寫一個測試使用的類
package localhost.webserviceTest.test;
import java.io.InterruptedIOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.junit.Test;
public class Httptest {
@Test
public void communicate() throws Exception {
byte[] bufSource = null;
String serviceUrl = "";
// HTTPConnection connection = null;
PostMethod method = null;
try {
serviceUrl = "http://localhost:8080/HttpServicePro/test/x";
String strSend = "zhengfang.sun requesting";
//新建連接類
HttpClient client = new HttpClient();
//新建連接屬性類
HttpClientParams clientParams = new HttpClientParams();
int soTimeout = 1000;// 返回超時時間
int connectionManagerTimeout = 1000;// 連接超時時間
//設置各個屬性
clientParams.setSoTimeout(soTimeout);
clientParams.setConnectionManagerTimeout(connectionManagerTimeout);
clientParams.setContentCharset("GBK");
client.setParams(clientParams);
client.getHttpConnectionManager().getParams().setConnectionTimeout(connectionManagerTimeout);
method = new PostMethod(serviceUrl);
//在這裏我們將我們的請求信息(一般來說是XML或者是JSON的報文)放入到PostMethod中
method.setRequestBody(strSend);
//設置調用時候的頭信息
method.setRequestHeader("Content-type", "text/xml; charset=GBK");
//執行調用
int status = client.executeMethod(method);
} catch (Exception e1) {
throw new Exception(" 系統連接失敗! ", new InterruptedIOException());
}
// 取得接受返回消息
bufSource = method.getResponseBody();
System.out.println("response ===== "+new String(bufSource,"GBK"));
method.releaseConnection();
}
}
然後我們就能通過調用我們之前的那個servlet進行簡單的測試了