Salinan kode adalah sebagai berikut:
impor java.io.bufferedReader;
impor java.io.ioException;
impor java.io.inputstream;
impor java.io.inputStreamReader;
impor org.apache.commons.httpclient.defaulthtpMethodRetryHandler;
impor org.apache.commons.httpclient.header;
impor org.apache.commons.httpclient.httpClient;
impor org.apache.commons.httpclient.httpException;
impor org.apache.commons.httpclient.httpstatus;
impor org.apache.commons.httpclient.namevaluepair;
impor org.apache.commons.httpclient.cookie.cookiepolicy;
impor org.apache.commons.httpclient.methods.getmethod;
impor org.apache.commons.httpclient.methods.postmethod;
impor org.apache.commons.httpclient.params.httpmethodparams;
TestHttpClient kelas publik {
public static void main (string [] args) {
// TODO Stub Metode yang dihasilkan otomatis
// Tentukan contoh httpclient
HttpClient httppClient = httpclient baru ();
// Buat instance metode GET
GetMethod getMethod = baru getMethod ("http://vevb.com");
// Gunakan kebijakan pemulihan default yang disediakan oleh sistem
//getmethod.getParams().setParameter(httpMethodparams.retry_handler, DEFAULTHTTPMETHODRETRYHANDLER ());
// Buat contoh metode postingan
PostMethod PostMethod = UTF8PostMethod baru ("http://vevb.com");
//
//// Isi nilai dari setiap bidang formulir
// namevaluePair [] data = {new namevaluepair ("user_name", "user_name"), new namevaluepair ("kata sandi", "kata sandi")};
//
//// Masukkan nilai formulir ke dalam metode postingan
//postmethod.setRequestBody(data);
//
//postmethod.getParams (). setParameter (
//"http.protocol.cookie-policy".cookiepolicy.browser_compatibility);
//postmethod.setRequestheader("referer "," http://vevb.com ");
mencoba{
// Jalankan metode GET
// int statuscode = httpclient.executeMethod (getMethod);
// Jalankan metode posting
int statuscode = httpclient.executeMethod (postmethod);
if (statuscode == httpstatus.sc_moved_temporly) {
Header locationHeader = postmethod.getResponseHeader ("location");
Lokasi String = NULL;
if (locationHeader! = null) {
location = locationHeader.getValue ();
}
postmethod = postmethod baru (lokasi);
postmethod.setrequestheader ("referer", "http://vevb.com/login");
NamevaluePair [] data1 = {new namevaluepair ("user_name", "user_name"), new namevaluepair ("kata sandi", "kata sandi")};
postmethod.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 ("Metode salah" + postmethod.getstatusLine ());
}
}
if (statuscode! = httpstatus.sc_ok) {
System.out.println ("Metode salah" + postmethod.getstatusLine ());
}
InputStream responseBody = postmethod.getResponseBodyAsstream ();
BufferedReader Reader = BufferedReader baru (InputStreamReader baru (ResponseBody, "UTF-8")));
String line = reader.readline ();
while (line! = null) {
System.out.println (string baru (line.getBytes ()));
line = reader.readline ();
}
}
Catch (httpException e) {
// todo: menangani pengecualian
System.out.println ("Silakan periksa alamat http yang disediakan!");
e.printstacktrace ();
} catch (ioException e) {
// todo: menangani pengecualian
System.out.println ("Garis salah!");
e.printstacktrace ();
} Akhirnya{
getMethod.releaseconnection (); // Lepaskan tautannya
postmethod.releaseconnection ();
}
}
// Kelas Dalam untuk Dukungan UTF-8
Public Static Class UTF8PostMethod memperluas postmethod {
UTF8PostMethod publik (string url) {
super (url);
}
@Mengesampingkan
string publik getRequestCharset () {
// return super.getRequestCharset ();
mengembalikan "UTF-8";
}
}
}