2016-04-04 13 views
0

Hallo ich versuche, den Response-Code von einer HTTPS-URLWie Antwortcode von einem HTTPS-URL erhalten java

import java.net.HttpURLConnection; 
import java.net.URL; 
import java.net.URLConnection; 
import java.io.*; 

import javax.net.ssl.HttpsURLConnection; 

import org.apache.commons.codec.binary.Base64; 

public class test 
{ 
    public static void main(String[] args) 
    throws Exception 
    { 
    /* String userpass = "NUPuMYCGIupzhbIe05cIwH2CcIQHXlh6" + ":" + "keG4sZAP82dcJK8H"; 
     String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes()); 
*/ 
    String httpsURL = "https://api.devo.com/ad/v1"; 
    URL url = new URL (httpsURL); 
    URLConnection connection = url.openConnection(); 
    //connection.setRequestProperty("Authorization", "Basic "+"lVQdU1ZQ0dJdX6aGJJZTAYl3SDJDY0lRSFhsaDY6a2VHNHNaQVA4MmRjSks4SA=="); 
    //String encoded = Base64.encode(username+":"+password); 


    connection.connect(); 

    // Cast to a HttpURLConnection 
    if (connection instanceof HttpURLConnection) 
    { 
     HttpURLConnection httpConnection = (HttpURLConnection) connection; 

     int code = httpConnection.getResponseCode(); 

     System.out.println(code); 
    } 
    else 
    { 
     System.err.println ("error - not a http request!"); 
    } 
    } 
} 

Ohne grundlegende auth Antwortcode bin immer als 401 zu holen, aber wenn ich grundlegende auth wie Sie verwenden sehe ich diejenigen aus

//connection.setRequestProperty("Authorization", "Basic "+"lVQdU1ZQ0ddXB6aGJJZTA1Y0l3SDJDY0lRSFhsaDY6a2VNHNaQVA4MmRjSks4SA=="); 

ich erhalte 500 als Code kommentiert, aber wenn die URL bin, ist unten 400 jetzt bekommen, wie kann ich den Grund auth passieren und warum mir immer 500 als Code, wenn ich mit Grunde auth passieren? Bitte schlagen Sie mir

Antwort

0

Ich schlage vor, Sie die Apache Http Komponenten. Werfen Sie einen Blick here

+0

wird es für https auch funktionieren? –

+0

ja, siehe http://hc.apache.org/httpclient-3.x/sslguide.html – borras

+0

Es funktionierte für mich, indem ich unconnection einfach connection.setRequestProperty ("Authorization", "Basic" + "lVQdU1ZQ0dJdX6aGJJZTAYl3SDJDY0lRSFhsaDY6a2VHNHNaQVA4MmRjSks4SA ==") ; –