Dua metode berikut akan menggunakan Java untuk mengirim permintaan URL dan mendapatkan nilai dikembalikan oleh server.
Metode pertama:
Salinan kode adalah sebagai berikut:
impor org.apache.http.httpentity;
impor org.apache.http.httpresponse;
impor org.apache.http.namevaluepair;
impor org.apache.http.client.httpClient;
impor org.apache.http.client.entity.urlencodedformentity;
impor org.apache.http.client.methods.httppost;
impor org.apache.http.impl.client.defaulthttpClient;
impor org.apache.http.message.basicnamevaluePair;
impor org.apache.http.params.coreconnectionPnames;
impor org.apache.http.util.entityutils;
PublicStatiCStringsEndUrlRequest (StringUrlStr, StringParam1, StringParam2) ThrowsException {
StringTemptr = null;
HttpClientHttpClient = newDefaulthttpClient ();
PropertiesProperties = newProperties ();
Httpentityentity = null;
StringXmlContent = "";
mencoba
{
// atur waktu batas waktu
httpclient.getParams (). setintparameter (coreconnectionpnames.connection_timeout, 20000);
httpclient.getParams (). setParameter (coreconnectionpnames.so_timeout, 20000);
// enkapsulasi parameter yang perlu dilewati
Daftar <namevaluePair> nvps = newArrayList <namevaluePair> ();
nvps.add (newbasicnamevaluePair ("mainMemocode", strmainmemocode));
nvps.add (newbasicnamevaluePair ("recordpassword", strrecordpassword));
// jenis metode permintaan klien
Httpposthttppost = newHttppost (urlstr);
httppost.setentity (newurlencodedformentity (nvps, "gbk"));
HttpresponseSponse = httpclient.execute (httppost);
// Dapatkan nilai tipe konten yang dikembalikan server ke http
tempstr = response.getheaders ("tipe konten") [0] .getValue (). ToString ();
// Dapatkan nilai halaman yang dikembalikan oleh server
entitas = response.getEntity ();
xmlcontent = entityutils.tostring (entitas);
StringstrMessage = null;
System.out.println (XMLContent);
System.out.println (response.getHeaders ("tipe konten") [0] .getValue (). ToString ());
httppost.abort ();
}
Catch (SocketTimeOutExceptione)
{
}
Catch (Exceptionex)
{
Ex.PrintStackTrace ();
}
Akhirnya{
httpclient.getConnectionManager (). shutdown ();
}
Metode kedua:
Salinan kode adalah sebagai berikut:
PublicStatiCStringsEndUrlRequest (StringUrlStr, StringParam1, StringParam2) ThrowsException {
HttpurlConnectionUrl_con = null;
mencoba{
Urlurl = newurl (urlstr);
StringBufferBankXmlBuffer = newstringBuffer ();
// Buat koneksi URL, kirimkan ke data, dan dapatkan hasil pengembalian
HttpurlConnectionConnection = (httpurlConnection) url.openconnection ();
Connection.setRequestMethod ("Post");
Connection.setDoOutput (true);
Connection.setRequestProperty ("agen-pengguna", "DirectClient");
Printwriterout = newPrintWriter (newoutputStreamWriter (connection.getoutputStream (), "gbk"));
out.println (param);
out.close ();
BufferedReaderin = newBufferedReader (newInputStreamReader (koneksi
.getInputStream (), "gbk"));
StringInputline;
while ((inputline = in.readline ())! = null) {
BankXmlBuffer.Append (Inputline);
}
melampirkan();
Tempstr = BankXmlBuffer.ToString ();
}
Catch (Exceptione)
{
System.out.println ("Pengecualian terjadi saat mengirim permintaan mendapatkan!" +E);
e.printstacktrace ();
} Akhirnya{
if (url_con! = null)
url_con.disconnect ();
}
returnmpestr;
}