資源簡介
http客戶端java程序,代碼質(zhì)量高,經(jīng)過測試。
代碼片段和文件信息
package?com.tcl.dhome.server.protocol.http;
import?java.io.BufferedInputStream;
import?java.io.BufferedOutputStream;
import?java.io.ByteArrayInputStream;
import?java.io.FileNotFoundException;
import?java.io.IOException;
import?java.io.InputStream;
import?java.net.InetAddress;
import?java.net.MalformedURLException;
import?java.net.ProtocolException;
import?java.net.Socket;
import?java.net.URL;
import?java.net.UnknownHostException;
import?java.util.Enumeration;
import?java.util.Properties;
public?class?HttpClient{
????protected?Socket?socket;
????protected?BufferedOutputStream?sender;?
????protected?BufferedInputStream?receiver;
????protected?ByteArrayInputStream?byteStream;???
??
????protected?URL?targetUrl;???
??
????private?int?responseCode?=?-1;?
????private?String?responseMessage?=?““;
????private?String?serverVersion?=?““;???
?
????private?int?responseLength?=0;
????
????private?Properties?headerProp?=?new?Properties();
??
????public?HttpClient(){}
??
????public?HttpClient(String?url){
????????GET(url);
????}???
??
??
????/*?GET方法根據(jù)URL,會請求文件、數(shù)據(jù)庫查詢結(jié)果、程序運行結(jié)果等多種內(nèi)容?*/??
??
????public?void?GET(String?url){???
??????try{?
????????????checkHTTP(url);
????????????openServer(targetUrl.getHost()targetUrl.getPort());
????????????String?cmd?=?“GET?“?+?getURLFormat(targetUrl)?+?“?HTTP/1.0\r\n“?
?????????????????????????????????????????????+?getbaseHeads()?+?“\r\n“;
????????????sendMessage(cmd);??
????????????receiveMessage();?
????????}
????????catch(ProtocolException?p){
????????????p.printStackTrace();
????????????return;
????????}
????????catch(UnknownHostException?e){
????????????e.printStackTrace();
????????????return;?
????????}?
????????catch(IOException?i){
????????????i.printStackTrace();
????????????return;
????????}?
????}???
??
??
????/*?
?????*?HEAD方法只請求URL的元信息,不包括URL本身。若懷疑本機和服務(wù)器上的?
?????*?文件相同,用這個方法檢查最快捷有效。?
?????*/
????public?void?HEAD(String?url){
????????try{?
????????????checkHTTP(url);
????????????openServer(targetUrl.getHost()targetUrl.getPort());
????????????String?cmd?=?“HEAD?“?+?getURLFormat(targetUrl)?+?“?HTTP/1.0\r\n“??
?????????????????????????????????????????????+?getbaseHeads()?+?“\r\n“;?
????????????sendMessage(cmd);
????????????receiveMessage();
????????}??
????????catch(ProtocolException?p){
????????????p.printStackTrace();
????????????return;?
????????}
????????catch(UnknownHostException?e){
????????????e.printStackTrace();
????????????return;
????????}
????????catch(IOException?i){?
????????????i.printStackTrace();?
????????????return;?
????????}?
????}?
????
????/*?
?????*?POST方法是向服務(wù)器傳送數(shù)據(jù),以便服務(wù)器做出相應(yīng)的處理。例如網(wǎng)頁上常用的?
?????*?提交表格。
?????*/?
????public?void?POST(String?url?String?content){?
????????try{
????????????checkHTTP(url);
????????????openServer(targetUrl.getHost()targetUrl.getPort());?
????????????String?cmd?=?“POST?“?+?getURLFormat(targetUrl)?+?“?HTTP/1.0\r\n“??
?????????????????????????????????????????+?getbaseHea
評論
共有 條評論