SORU
24 Temmuz 2010, CUMARTESİ


Gönderme HTTP POST Java Talep

sağlar varsayalım bu URL...

http://www.example.com/page.php?id=10            

(Burada kimliği bir POST isteği gönderilmesi gerekiyor)

page.php, POST yöntemi olarak kabul sunucuya id = 10 göndermek istiyorum.

Nasıl Java içinde olabilir mi?

Bunu denedim :

URL aaa = new URL("http://www.example.com/page.php");
URLConnection ccc = aaa.openConnection();

Ama yine de posta ile göndermek için nasıl anlamaya olamaz

CEVAP
24 Temmuz 2010, CUMARTESİ


Güncelleme Cevap:

Bu sınıflardan bazıları, orijinal cevap, Apache HTTP Bileşenleri daha yeni sürümü kaldırılmış olduğundan, bu güncelleştirme post ediyorum.

Bu arada, daha fazla örnek için tam Dokümantasyon here erişebilirsiniz.

HttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost("http://www.a-domain.com/foo/");

// Request parameters and other properties.
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("param-1", "12345"));
params.add(new BasicNameValuePair("param-2", "Hello!"));
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

//Execute and get the response.
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();

if (entity != null) {
    InputStream instream = entity.getContent();
    try {
        // do something useful
    } finally {
        instream.close();
    }
}

Orijinal Cevabı:

Apache HttpClient kullanmanızı tavsiye ederim. ve uygulamak için daha hızlı, daha kolay.

PostMethod post = new PostMethod("http://jakarata.apache.org/");
        NameValuePair[] data = {
          new NameValuePair("user", "joe"),
          new NameValuePair("password", "bloggs")
        };
        post.setRequestBody(data);
        // execute method and handle any error responses.
        ...
        InputStream in = post.getResponseBodyAsStream();
        // handle response.

daha fazla bilgi için bu url kontrol etmek için: http://hc.apache.org/

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • dcigs

    dcigs

    9 EYLÜL 2006
  • Film Riot

    Film Riot

    16 NİSAN 2006
  • lissaandbeauty

    lissaandbeau

    24 Aralık 2011