コードコピーは次のとおりです。
java.io.bufferedreaderをインポートします。
java.io.ioexceptionをインポートします。
java.io.inputStreamをインポートします。
java.io.inputStreamReaderをインポートします。
org.apache.commons.httpclient.defaulthttpmethodretryhandlerをインポートします。
org.apache.commons.httpclient.headerをインポートします。
Import org.apache.commons.httpclient.httpclient;
Import org.apache.commons.httpclient.httpexception;
org.apache.commons.httpclient.httpstatusをインポートします。
org.apache.commons.httpclient.namevaluepairをインポートします。
org.apache.commons.httpclient.cookie.cookiepolicyをインポートします。
org.apache.commons.httpclient.methods.getMethod;
org.apache.commons.httpclient.methods.postmethodをインポートします。
org.apache.commons.httpclient.params.httpmethodparamsをインポートします。
public class testhttpclient {
public static void main(string [] args){
// TODO自動生成方法スタブ
// httpclientのインスタンスを定義します
httpclient httppclient = new httpclient();
// GETメソッドのインスタンスを作成します
getMethod getMethod = new getMethod( "http://vevb.com");
//システムが提供するデフォルトの回復ポリシーを使用します
//getMethod.getParams().setParameter(httpmethodparams.retry_handler、new defaulthtpmethodretryhandler());
// postメソッドインスタンスを作成します
Method postmethod = new utf8postmethod( "http://vevb.com");
//
////各フォームフィールドの値を入力します
// nameValuePair [] data = {new nameValuePair( "user_name"、 "user_name")、new nameValuePair( "password"、 "password")};
//
////フォームの値をPOSTメソッドに入れます
//postmethod.setRequestBody(Data);
//
//postmethod.getParams()。setParameter(
//"http.protocol.cookie-policy",cookiepolicy.browser_compatibility);
//postmethod.setRequestheader("Referer "、" http://vevb.com ");
試す{
// getメソッドを実行します
// int statusCode = httpclient.executeMethod(getMethod);
// postメソッドを実行します
int statusCode = httpclient.executemethod(postmethod);
if(statuscode == httpstatus.sc_moved_temporily){
Header LocationHeader = postmethod.getResponseHeader( "location");
文字列location = null;
if(locationheader!= null){
location = locationheader.getValue();
}
postmethod = new postmethod(location);
postmethod.setRequestheader( "参照"、 "http://vevb.com/login");
namevaluepair [] data1 = {new nameValuePair( "user_name"、 "user_name")、new nameValuePair( "password"、 "password")};
method.setRequestBody(data1);
postmethod.getParams()。setParameter(
「http.protocol.cookie-policy」、cookiepolicy.browser_compatibility);
int statuscode1 = httpclient.executemethod(postmethod);
if(statuscode1!= httpstatus.sc_ok){
system.out.println( "メソッドは間違っています" + postmethod.getStatusline());
}
}
if(statuscode!= httpstatus.sc_ok){
system.out.println( "メソッドは間違っています" + postmethod.getStatusline());
}
inputstream responsebody = postmethod.getResponseBodyAsstream();
BufferedReader Reader = new BufferedReader(new inputStreamReader(ResponseBody、 "UTF-8"));
string line = reader.readline();
while(line!= null){
System.out.println(new String(line.getBytes()));
line = reader.readline();
}
}
catch(httpexceptione){
// TODO:例外を処理します
System.out.println( "提供されているHTTPアドレスを確認してください!");
e.printstacktrace();
} catch(ioException e){
// TODO:例外を処理します
system.out.println( "ラインが間違っています!");
e.printstacktrace();
} ついに{
getMethod.releaseConnection(); //リンクをリリースします
postmethod.releaseconnection();
}
}
// UTF-8サポートの内部クラス
public static class utf8postmethod extends postmethod {
public utf8postmethod(string url){
super(url);
}
@オーバーライド
public string getRequestCharset(){
// super.getRequestcharset()を返します。
「UTF-8」を返します。
}
}
}