JDKは、ステートレスプロトコル要求(HTTP)のサポートを提供します。
まず、リクエストクラス(httprequester)を作成しましょう。
このクラスは、次のように、Javaのコードをカプセル化して簡単な要求を実装します。
コードコピーは次のとおりです。
java.io.bufferedreaderをインポートします。
java.io.ioexceptionをインポートします。
java.io.inputStreamをインポートします。
java.io.inputStreamReaderをインポートします。
java.net.httpurlconnectionをインポートします。
java.net.urlをインポートします。
java.nio.charset.charsetをインポートします。
java.util.mapをインポートします。
java.util.vectorをインポートします。
/**
* HTTP要求オブジェクト
*
* @author yymmiinngg
*/
パブリッククラスhttprequester {
プライベート文字列defaultcontentencoding;
public httprequester(){
this.defaultContentencoding = charset.defaultCharset()。name();
}
/**
* GETリクエストを送信します
*
* @param urlstring
* URLアドレス
* @return応答オブジェクト
* @throws ioexception
*/
public httprespons sendget(string urlstring)throws ioexception {
this.send(urlstring、 "get"、null、null)を返します。
}
/**
* GETリクエストを送信します
*
* @param urlstring
* URLアドレス
* @param params
*パラメーターセット
* @return応答オブジェクト
* @throws ioexception
*/
public httprespons sendget(string urlstring、map <string、string> params)
IOExceptionをスローする{
this.send(urlstring、 "get"、params、null)を返します。
}
/**
* GETリクエストを送信します
*
* @param urlstring
* URLアドレス
* @param params
*パラメーターセット
* @paramプロパティ
*属性を要求します
* @return応答オブジェクト
* @throws ioexception
*/
public httprespons sendget(string urlstring、map <string、string> params、
map <string、string> properties)throws ioexception {
this.send(urlstring、 "get"、params、properties)を返します。
}
/**
* POSTリクエストを送信します
*
* @param urlstring
* URLアドレス
* @return応答オブジェクト
* @throws ioexception
*/
public httprespons sendpost(string urlstring)throws ioexception {
this.send(urlstring、 "post"、null、null)を返します。
}
/**
* POSTリクエストを送信します
*
* @param urlstring
* URLアドレス
* @param params
*パラメーターセット
* @return応答オブジェクト
* @throws ioexception
*/
public httprespons sendpost(string urlstring、map <string、string> params)
IOExceptionをスローする{
this.send(urlstring、 "post"、params、null)を返します。
}
/**
* POSTリクエストを送信します
*
* @param urlstring
* URLアドレス
* @param params
*パラメーターセット
* @paramプロパティ
*属性を要求します
* @return応答オブジェクト
* @throws ioexception
*/
public httprespons sendpost(string urlstring、map <string、string> params、
map <string、string> properties)throws ioexception {
this.send(urlstring、 "post"、params、properties)を返します。
}
/**
* HTTPリクエストを送信します
*
* @param urlstring
* @return反射オブジェクト
* @throws ioexception
*/
プライベートhttprespons send(string urlstring、string method、
map <string、string>パラメーター、map <string、string>プロパティ)
IOExceptionをスローする{
httpurlconnection urlconnection = null;
if(method.equalsignorecase( "get")&& parameters!= null){
stringbuffer param = new StringBuffer();
int i = 0;
for(string key:parameters.keyset()){
if(i == 0)
param.append( "?");
それ以外
param.append( "&");
param.append(key).append( "=")。append(parameters.get(key));
i ++;
}
urlstring += param;
}
url url = new url(urlstring);
urlconnection =(httpurlconnection)url.openconnection();
urlconnection.setRequestMethod(メソッド);
urlconnection.setDoOutput(true);
urlconnection.setDoinput(true);
urlconnection.setusecaches(false);
if(propertys!= null)
for(string key:properties.keyset()){
urlconnection.addrequestproperty(key、properties.get(key));
}
if(method.equalsignorecase( "post")&& parameters!= null){
stringbuffer param = new StringBuffer();
for(string key:parameters.keyset()){
param.append( "&");
param.append(key).append( "=")。append(parameters.get(key));
}
urlconnection.getOutputStream()。write(param.toString()。getBytes());
urlconnection.getOutputStream()。flush();
urlconnection.getOutputStream()。close();
}
this.makecontent(urlstring、urlconnection)を返します。
}
/**
*応答オブジェクトを取得します
*
* @param urlconnection
* @return応答オブジェクト
* @throws ioexception
*/
プライベートhttprespons makecontent(string urlstring、
httpurlconnection urlconnection)throws ioexception {
httprespons httpresponser = new httprespons();
試す {
inputstream in = urlconnection.getInputStream();
BufferedReader BufferedReader = new BufferedReader(
new inputStreamReader(in));
httpresponser.contentCollection = new Vector <String>();
stringbuffer temp = new StringBuffer();
string line = bufferedreader.readline();
while(line!= null){
httpresponser.contentcollection.add(line);
Temp.Append(line).append( "/r/n");
line = bufferedreader.readline();
}
bufferedreader.close();
string ecod = urlconnection.getContentenCoding();
if(ecod == null)
ecod = this.defaultContentencoding;
httpresponser.urlstring = urlstring;
httpresponser.defaultport = urlconnection.geturl()。getDefaultPort();
httpresponser.file = urlconnection.geturl()。getFile();
httpresponser.host = urlconnection.geturl()。gethost();
httpresponser.path = urlconnection.geturl()。getPath();
httpresponser.port = urlconnection.geturl()。getport();
httpresponser.protocol = urlconnection.geturl()。getProtocol();
httpresponser.query = urlconnection.geturl()。getQuery();
httpresponser.ref = urlconnection.geturl()。getRef();
httpresponser.userinfo = urlconnection.geturl()。getuserinfo();
httpresponser.content = new String(temp.toString()。getBytes()、ecod);
httpresponser.contentencoding = ecod;
httpresponser.code = urlconnection.getResponseCode();
httpresponser.message = urlconnection.getResponsemessage();
httpresponser.contenttype = urlconnection.getContentType();
httpresponser.method = urlconnection.getRequestMethod();
httpresponser.connecttimeout = urlconnection.getConnectTimeout();
httpresponser.readtimeout = urlconnection.getReadTimeout();
httpresponserを返します。
} catch(ioException e){
eを投げる;
} ついに {
if(urlconnection!= null)
urlconnection.disconnect();
}
}
/**
*デフォルトの応答文字セット
*/
public string getDefaultContentenCoding(){
this.defaultContentencodingを返します。
}
/**
*デフォルトの応答文字セットを設定します
*/
public void setDefaultContentEncoding(String DefaultContentEncoding){
this.defaultContentEncoding = defaultContentEncoding;
}
}
第二に、応答オブジェクト(httprespons)を見てみましょう。 応答オブジェクトは、実際には単なるデータビーンであり、次のようにリクエスト応答の結果データをカプセル化します。
コードコピーは次のとおりです。
java.util.vectorをインポートします。
/**
*応答オブジェクト
*/
パブリッククラスhttprespons {
文字列urlstring;
int defaultport;
文字列ファイル;
文字列ホスト;
文字列パス;
intポート;
文字列プロトコル;
文字列クエリ;
文字列ref;
string userinfo;
文字列contentencoding;
文字列コンテンツ。
文字列contentType;
intコード;
文字列メッセージ;
文字列メソッド;
int connecttimeout;
int readtimeout;
vector <string> contentCollection;
public string getContent(){
コンテンツを返す;
}
public string getContentType(){
contentTypeを返します。
}
public int getCode(){
返品コード。
}
public string getMessage(){
メッセージを返します。
}
public vector <string> getContentCollection(){
ContentCollectionを返します。
}
public string getContentEncoding(){
contentencodingを返します。
}
public string getMethod(){
返品方法;
}
public int getConnectTimeout(){
connecttimeoutを返します。
}
public int getReadTimeout(){
ReadTimeOutを返します。
}
public string geturlstring(){
urlstringを返します。
}
public int getDefaultPort(){
defaultportを返します。
}
public string getFile(){
ファイルを返します。
}
public string gethost(){
ホストを返します。
}
public string getPath(){
戻りパス。
}
public int getport(){
返品ポート。
}
public string getProtocol(){
返品プロトコル。
}
public string getQuery(){
クエリを返す;
}
public string getRef(){
return ref;
}
public string getUserInfo(){
userInfoを返します。
}
}
最後に、上記のコードが正しいかどうかをテストするためのアプリケーションクラスを書きましょう
コードコピーは次のとおりです。
com.yao.http.httprequesterをインポートします。
com.yao.http.httpresponsをインポートします。
パブリッククラステスト{
public static void main(string [] args){
試す {
httprequester request = new httprequester();
httprespons hr = request.sendget( "// www.vevb.com");
system.out.println(hr.geturlstring());
System.out.println(hr.getProtocol());
System.out.println(hr.gethost());
System.out.println(hr.getport());
system.out.println(hr.getContentenCoding());
System.out.println(hr.getMethod());
System.out.println(hr.getContent());
} catch(例外e){
e.printstacktrace();
}
}
}