मैं क्रॉलर बना रहा हूं, और अगर यह 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);
}
}
"इनपुटस्ट्रीम = connection.getResponseMessage();" मुझे URLConnection क्लास में getResponseMessage विधि नहीं दिखाई देती है, यह HttpUrlConnection का हिस्सा है, इसलिए क्या हमें टाइपकास्ट नहीं करना चाहिए? या हम getInputStream के साथ getResponseMessage को प्रतिस्थापित कर सकते हैं, या क्या यह अपवाद फेंक देगा? – David
यह एक टाइपो था, यह 'connection.getInputStream()' है। –
बहुत अच्छा और छोटा जवाब –