نسخة الكود كما يلي:
استيراد java.io.bufferedreader ؛
استيراد java.io.ioException ؛
استيراد java.io.inputstream ؛
استيراد java.io.inputstreamreader ؛
استيراد org.apache.commons.httpclient.defaulthttpmethodretryhandler ؛
استيراد org.apache.commons.httpclient.header ؛
استيراد org.apache.commons.httpclient.httpclient ؛
استيراد 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 ؛
الفئة العامة testHttpClient {
الفراغ الثابت العام الرئيسي (سلسلة [] args) {
// TODO METHOTION METTOD COBS
// تحديد مثيل httpclient
httpclient httppclient = new httpclient () ؛
// قم بإنشاء مثيل لطريقة GET
getMethod getMethod = getMethod جديد ("http://vevb.com") ؛
// استخدم سياسة الاسترداد الافتراضية التي يوفرها النظام
//getmethod.getParams().setParameter(httpmethodparams.retry_handler ، new DefaulThtpMethodretryHandler ()) ؛
// إنشاء مثيل طريقة نشر
postmethod postmethod = جديد UTF8PostMethod ("http://vevb.com") ؛
//
////املأ قيم كل حقل نموذج
// namevaluepair [] data = {new nameValUepair ("user_name" ، "user_name") ، new nameValuePair ("password" ، "password")} ؛
//
////ضع قيمة النموذج في طريقة النشر
//postmethod.setRequestbody(data) ؛
//
//postmethod.getparams (). setParameter (
//"http.protocol.cookie-policy"،oCoOkiePolicy.browser_compatibility) ؛
//postmethod.setRequestHeader("referer "،" http://vevb.com ") ؛
يحاول{
// تنفيذ طريقة الحصول
// int statusCode = httpclient.executemethod (getMethod) ؛
// تنفيذ طريقة البريد
int statusCode = httpclient.executemethod (postmethod) ؛
if (statusCode == httpstatus.sc_move_temporialy) {
header locationheader = postmethod.getResponseHeader ("الموقع") ؛
موقع السلسلة = فارغ ؛
if (locationheader! = null) {
الموقع = موقع header.getValue () ؛
}
postmethod = جديد postmethod (الموقع) ؛
postmethod.setRequestHeader ("المرجع" ، "http://vevb.com/login") ؛
nameValuePair [] data1 = {new nameValUepair ("user_name" ، "user_name") ، new nameValUepair ("password" ، "password")} ؛
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 ("الطريقة خاطئة" + postmethod.getStatusLine ()) ؛
}
}
if (statusCode! = httpstatus.sc_ok) {
System.out.println ("الطريقة خاطئة" + postmethod.getStatusLine ()) ؛
}
inputStream reviewBody = postmethod.getResponseBodyassTream () ؛
BufferedReader Reader = جديد BufferEdReader (New InputStreamReader (ResponseBody ، "UTF-8")) ؛
خط السلسلة = reader.ReadLine () ؛
بينما (الخط! = فارغ) {
System.out.println (سلسلة جديدة (line.getBytes ())) ؛
line = reader.ReadLine () ؛
}
}
catch (httpexception e) {
// تودو: التعامل مع الاستثناء
System.out.println ("يرجى التحقق من عنوان HTTP المقدم!") ؛
E.PrintStackTrace () ؛
} catch (ioException e) {
// تودو: التعامل مع الاستثناء
System.out.println ("الخط خاطئ!") ؛
E.PrintStackTrace () ؛
} أخيراً{
getMethod.releaseconnection () ؛ // حرر الرابط
postmethod.releaseconnection () ؛
}
}
// الفصل الداخلي لدعم UTF-8
الطبقة الثابتة العامة utf8postmethod يمتد postmethod {
العام utf8postmethod (url string) {
سوبر (url) ؛
}
@تجاوز
السلسلة العامة getRequestcharset () {
// return super.getRequestcharset () ؛
إرجاع "UTF-8" ؛
}
}
}