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

  • aki6336

    aki6336

    14 AĞUSTOS 2008
  • Matt Harding

    Matt Harding

    23 Mayıs 2006
  • The Slow Mo Guys

    The Slow Mo

    15 AĞUSTOS 2010