2012-02-03 12 views
17

मैं क्रॉलर बना रहा हूं, और अगर यह 200 है या नहीं, तो स्ट्रीम से डेटा प्राप्त करने की आवश्यकता है। कर्ल यह कर रहा है, साथ ही साथ कोई मानक ब्राउज़र भी कर रहा है।URLConnection मुझे एचटीपी त्रुटियों (404,500, आदि) पर डेटा तक पहुंचने की इजाजत नहीं दे रहा है

निम्नलिखित में वास्तव में अनुरोध की सामग्री नहीं मिलेगी, भले ही कुछ है, http त्रुटि स्थिति कोड के साथ एक अपवाद फेंक दिया गया है। मैं आउटपुट चाहता हूं, क्या कोई रास्ता है? मैं इस पुस्तकालय का उपयोग करना पसंद करता हूं क्योंकि यह वास्तव में लगातार कनेक्शन करेगा, जो कि मैं क्रॉलिंग के प्रकार के लिए बिल्कुल सही हूं।

package test; 

import java.net.*; 
import java.io.*; 

public class Test { 

    public static void main(String[] args) { 

     try { 

      URL url = new URL("http://github.com/XXXXXXXXXXXXXX"); 
      URLConnection connection = url.openConnection(); 

      DataInputStream inStream = new DataInputStream(connection.getInputStream()); 
      String inputLine; 

      while ((inputLine = inStream.readLine()) != null) { 
       System.out.println(inputLine); 
      } 
      inStream.close(); 
     } catch (MalformedURLException me) { 
      System.err.println("MalformedURLException: " + me); 
     } catch (IOException ioe) { 
      System.err.println("IOException: " + ioe); 
     } 
    } 
} 

काम किया, धन्यवाद: यहाँ है कि मैं क्या के साथ आया है - बस अवधारणा का एक मोटा सबूत के रूप में:

import java.net.*; 
import java.io.*; 

public class Test { 

    public static void main(String[] args) { 
//InputStream error = ((HttpURLConnection) connection).getErrorStream(); 

     URL url = null; 
     URLConnection connection = null; 
     String inputLine = ""; 

     try { 

      url = new URL("http://verelo.com/asdfrwdfgdg"); 
      connection = url.openConnection(); 

      DataInputStream inStream = new DataInputStream(connection.getInputStream()); 

      while ((inputLine = inStream.readLine()) != null) { 
       System.out.println(inputLine); 
      } 
      inStream.close(); 
     } catch (MalformedURLException me) { 
      System.err.println("MalformedURLException: " + me); 
     } catch (IOException ioe) { 
      System.err.println("IOException: " + ioe); 

      InputStream error = ((HttpURLConnection) connection).getErrorStream(); 

      try { 
       int data = error.read(); 
       while (data != -1) { 
        //do something with data... 
        //System.out.println(data); 
        inputLine = inputLine + (char)data; 
        data = error.read(); 
        //inputLine = inputLine + (char)data; 
       } 
       error.close(); 
      } catch (Exception ex) { 
       try { 
        if (error != null) { 
         error.close(); 
        } 
       } catch (Exception e) { 

       } 
      } 
     } 

     System.out.println(inputLine); 
    } 
} 

उत्तर

37

सरल:

URLConnection connection = url.openConnection(); 
InputStream is = connection.getInputStream(); 
if (connection instanceof HttpURLConnection) { 
    HttpURLConnection httpConn = (HttpURLConnection) connection; 
    int statusCode = httpConn.getResponseCode(); 
    if (statusCode != 200 /* or statusCode >= 200 && statusCode < 300 */) { 
    is = httpConn.getErrorStream(); 
    } 
} 

आप विवरण के लिए जावाडोक का उल्लेख कर सकते ।

URLConnection connection = url.openConnection(); 
InputStream is = null; 
try { 
    is = connection.getInputStream(); 
} catch (IOException ioe) { 
    if (connection instanceof HttpURLConnection) { 
     HttpURLConnection httpConn = (HttpURLConnection) connection; 
     int statusCode = httpConn.getResponseCode(); 
     if (statusCode != 200) { 
      is = httpConn.getErrorStream(); 
     } 
    } 
} 
+0

"इनपुटस्ट्रीम = connection.getResponseMessage();" मुझे URLConnection क्लास में getResponseMessage विधि नहीं दिखाई देती है, यह HttpUrlConnection का हिस्सा है, इसलिए क्या हमें टाइपकास्ट नहीं करना चाहिए? या हम getInputStream के साथ getResponseMessage को प्रतिस्थापित कर सकते हैं, या क्या यह अपवाद फेंक देगा? – David

+0

यह एक टाइपो था, यह 'connection.getInputStream()' है। –

+0

बहुत अच्छा और छोटा जवाब –

8

आप openConnection बुला के बाद निम्नलिखित कार्य करने होंगे: सबसे अच्छा तरीका है मैं इस संभाल होगा इस प्रकार है।

  1. HttpURLConnection

  2. कॉल getResponseCode

  3. को URLConnection कास्ट प्रतिक्रिया एक सफलता है, तो getInputStream का उपयोग करें, अन्यथा getErrorStream

(परीक्षण की सफलता के लिए उपयोग करना चाहिए 200 <= code < 300 हो क्योंकि 200 से अलग वैध HTTP सफलता कोड हैं।)


मैं एक क्रॉलर बनाने रहा हूँ, और भले ही अगर यह एक 200 या नहीं है धारा से डेटा प्राप्त करने की जरूरत है।

बस जागरूक रहें कि यदि कोड 4xx या 5xx है, तो "डेटा" किसी प्रकार का त्रुटि पृष्ठ होने की संभावना है।


अंतिम बात यह है कि किया जाना चाहिए कि आप हमेशा एक साइट जिनके स्वामियों की सामग्री को स्क्रैप "robots.txt" फ़ाइल का सम्मान करना चाहिए ... और रेंगने से पहले सेवा की शर्तें पढ़ें/है पराक्रम देखभाल। बस जीईटी अनुरोधों को बंद करने से साइट मालिकों को परेशान होने की संभावना है ... जब तक कि आप उनके साथ पहले से ही "व्यवस्था" नहीं कर पाएंगे।

संबंधित मुद्दे