SORU
4 EKİM 2012, PERŞEMBE


Ekleme HttpURLConnection için başlık

Benim istek HttpUrlConnection setRequestProperty() çalışma görünmüyor yöntemi kullanılarak başlık eklemek için çalışıyorum. Sunucu tarafı benim başlığı ile herhangi bir isteği kabul etmez. Biri bana yardım edebilir mi? Şimdiden teşekkürler.

HttpURLConnection hc;
    try {
        String authorization = "";
        URL address = new URL(url);
        hc = (HttpURLConnection) address.openConnection();


        hc.setDoOutput(true);
        hc.setDoInput(true);
        hc.setUseCaches(false);

        if (username != null && password != null) {
            authorization = username   ":"   password;
        }

        if (authorization != null) {
            byte[] encodedBytes;
            encodedBytes = Base64.encode(authorization.getBytes(), 0);
            authorization = "Basic "   encodedBytes;
            hc.setRequestProperty("Authorization", authorization);
        }

CEVAP
21 Mart 2013, PERŞEMBE


Geçmişte aşağıdaki kodu kullandım ve temel kimlik doğrulaması TomCat etkin çalışmış:

URL myURL = new URL(serviceURL);
HttpURLConnection myURLConnection = (HttpURLConnection)myURL.openConnection();
String userCredentials = "username:password";
String basicAuth = "Basic "   new String(new Base64().encode(userCredentials.getBytes()));
myURLConnection.setRequestProperty ("Authorization", basicAuth);
myURLConnection.setRequestMethod("POST");
myURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
myURLConnection.setRequestProperty("Content-Length", ""   Integer.toString(postData.getBytes().length));
myURLConnection.setRequestProperty("Content-Language", "en-US");
myURLConnection.setUseCaches(false);
myURLConnection.setDoInput(true);
myURLConnection.setDoOutput(true);

Yukarıdaki kodu deneyebilirsiniz. Yukarıdaki kod YAZI için, ve ALMAK için değiştirebilirsiniz

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

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • karneson

    karneson

    23 Temmuz 2006
  • PoreoticsHD

    PoreoticsHD

    22 NİSAN 2010
  • Techmoan

    Techmoan

    31 Mayıs 2009