สองวิธีต่อไปนี้จะใช้ Java เพื่อส่งคำขอ URL และรับค่าที่ส่งคืนโดยเซิร์ฟเวอร์
วิธีแรก:
การคัดลอกรหัสมีดังนี้:
นำเข้า org.apache.http.htttentity;
นำเข้า org.apache.http.httpresponse;
นำเข้า org.apache.http.namevaluePair;
นำเข้า org.apache.http.client.httpClient;
นำเข้า org.apache.http.client.entity.urlencodedformentity;
นำเข้า org.apache.http.client.methods.httppost;
นำเข้า org.apache.http.impl.client.defaulthttpClient;
นำเข้า org.apache.http.message.basicnamevaluepair;
นำเข้า org.apache.http.params.coreconnectionpnames;
นำเข้า org.apache.http.util.entityutils;
PublicStaticStringSendurlRequest (StringUrlstr, StringParam1, StringParam2) ThrowSexception {
StringTempStr = null;
httpClienthttpClient = newDefaulthttpClient ();
PropertiesProperties = newProperties ();
httpentityEntity = null;
StringxMlContent = "";
พยายาม
-
// ตั้งเวลาหมดเวลา
httpClient.getParams (). setintParameter (coreConnectionPnames.connection_timeout, 20000);
httpClient.getParams (). setParameter (coreConnectionPnames.so_timeout, 20000);
// การห่อหุ้มพารามิเตอร์ที่ต้องผ่าน
รายการ <namevaluePair> nvps = newarraylist <namevaluePair> ();
NVPS.ADD (newBasicNameValuePair ("MainMeMocode", strmainMeMocode));
nvps.add (newbasicnamevaluepair ("recordpassword", strrecordpassword));
// ประเภทวิธีการร้องขอไคลเอนต์
httPPOSTHTPPOST = newhttppost (urlstr);
httppost.setEntity (newUrlencodedFormentity (NVPS, "GBK"));
httpresponsesponse = httpClient.execute (httppost);
// รับค่าของประเภทเนื้อหาที่เซิร์ฟเวอร์กลับไปที่ http
tempstr = response.getheaders ("เนื้อหาประเภท") [0] .getValue (). toString ();
// รับค่าของหน้าเว็บที่ส่งคืนโดยเซิร์ฟเวอร์
entity = response.getEntity ();
xmlContent = entityUtils.toString (เอนทิตี);
stringstrMessage = null;
System.out.println (xmlcontent);
System.out.println (Response.getheaders ("เนื้อหาประเภท") [0] .getValue (). ToString ());
httppost.abort ();
-
จับ (SockettimeoutExceptione)
-
-
จับ (ExceptionEx)
-
Ex.PrintStackTrace ();
-
ในที่สุด{
httpClient.getConnectionManager (). shutdown ();
-
วิธีที่สอง:
การคัดลอกรหัสมีดังนี้:
PublicStaticStringSendurlRequest (StringUrlstr, StringParam1, StringParam2) ThrowSexception {
httpurlConnectionUrl_con = null;
พยายาม{
urlurl = newUrl (urlstr);
StringBufferBankxMlBuffer = NewStringBuffer ();
// สร้างการเชื่อมต่อ URL ส่งไปยังข้อมูลและรับผลตอบแทน
httpurlConnectionConnection = (httpurlConnection) url.openconnection ();
Connection.setRequestMethod ("โพสต์");
Connection.setDooutput (จริง);
Connection.setRequestProperty ("ผู้ใช้ตัวแทน", "DirectClient");
printWriterout = newPrintWriter (newOutputStreamWriter (Connection.getOutputStream (), "GBK"));
out.println (param);
out.close ();
bufferedReaderin = newBufferedReader (newInputStreamReader (การเชื่อมต่อ
.getInputStream (), "GBK"));
StringInputLine;
ในขณะที่ ((inputline = in.readline ())! = null) {
bankxmlbuffer.append (อินพุตไลน์);
-
in.close ();
tempstr = bankxmlbuffer.toString ();
-
จับ (ข้อยกเว้น)
-
System.out.println ("ข้อยกเว้นเกิดขึ้นเมื่อส่งคำขอรับ!" +e);
E.PrintStackTrace ();
} ในที่สุด{
if (url_con! = null)
url_con.disconnect ();
-
returnmpestr;
-