19 Mart 2012, PAZARTESİ
Nasıl HttpURLConnection POST kullanarak parametre ekleme
Yapmaya çalışıyorumPOSTHttpURLConnection
(Bu şekilde kullanmam gerekiyor, HttpPost
kullanamazsınız) ve Bağlantı gibi parametre olarak eklemek istiyorum
post.setEntity(new UrlEncodedFormEntity(nvp));
nerede
nvp = new ArrayList<NameValuePair>();
bazı verilerin saklı olması. Burada olan* *10 ArrayList
Bu eklemek için nasıl bir yol bulamıyorum:
HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
https.setHostnameVerifier(DO_NOT_VERIFY);
http = https;
http.setRequestMethod("POST");
http.setDoInput(true);
http.setDoOutput(true);
Garip https ve http kombinasyonu bunun nedeni gerekdoğrulanıyorbelgesi. Bunu sunucu iyi yayınlar olsa da bu bir sorun değildir. Ama bu bağımsız değişken ile post etmek istiyorum.
Herhangi bir fikir?
CEVAP
21 Kasım 2012, ÇARŞAMBA
Bağlantı için çıkış akışı için parametre sorgu dizesi yazabilirsiniz.
URL url = new URL("http://yoururl.com");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("firstParam", paramValue1));
params.add(new BasicNameValuePair("secondParam", paramValue2));
params.add(new BasicNameValuePair("thirdParam", paramValue3));
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(params));
writer.flush();
writer.close();
os.close();
conn.connect();
...
private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException
{
StringBuilder result = new StringBuilder();
boolean first = true;
for (NameValuePair pair : params)
{
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
}
return result.toString();
}
Bunu Paylaş:
Nasıl parametre olarak gönderilen bir ...
Nasıl javascript kullanarak dinamik ol...
Nasıl bir HTTP parametre ekleme OLSUN ...
Nasıl JSON POST isteği HTTPClient kull...
Nasıl ve HTML ve PHP ile birden fazla ...