2009-07-05 16 views
14
sock = new Socket("www.google.com", 80); 
     out = new BufferedOutputStream(sock.getOutputStream()); 
     in = new BufferedInputStream(sock.getInputStream()); 

सांत्वना देने जब मैं आप एक वेब पृष्ठ की सामग्री मुद्रित करने के लिए चाहते हैं, तो नीचे दिए गएजावा InputStream प्रिंट सामग्री

BufferedInputStream bin = new BufferedInputStream(in); 
int b; 
while ((b = bin.read()) != -1) 
{ 

    char c = (char)b;   

    System.err.print(""+(char)b); //This prints out content that is unreadable. 
            //Isn't it supposed to print out html tag? 
} 
+0

कृपया एक छोटा लेकिन * पूरा * उदाहरण दिखाएं। आपने यह नहीं दिखाया है कि आप Google को अनुरोध कैसे भेज रहे हैं। यदि आप निर्दिष्ट करते हैं कि आप gzipped डेटा को संभाल सकते हैं, उदाहरण के लिए, आपको पहले आउटपुट को डिकंप्रेस करना होगा। –

+0

(यह भी ध्यान रखें कि आपका वर्तमान कोड प्रभावी ढंग से आईएसओ-लैटिन -1 मान रहा है।) –

+0

हाय, मैं नई सॉकेट() खोलने के बाद; मैं "index.html प्राप्त करें" करता हूं और ऊपर दिए गए कोड की तरह "इन" प्राप्त करने का प्रयास करके इसे "आउट" पर भेजता हूं। मैंने gzipped को निर्दिष्ट नहीं किया है, यह पता लगाने के लिए कि क्या यह gzipped है? – cometta

उत्तर

18

की तरह "में" अंदर सामग्री से बाहर मुद्रण करने की कोशिश, आप की जरूरत है HTTP प्रोटोकॉल के साथ काम करें।

URL url = new URL("http","www.google.com"); 
HttpURLConnection urlc = (HttpURLConnection)url.openConnection(); 
urlc.setAllowUserInteraction(false); 
urlc.setDoInput(true); 
urlc.setDoOutput(false); 
urlc.setUseCaches(true); 
urlc.setRequestMethod("GET"); 
urlc.connect(); 
// check you have received an status code 200 to indicate OK 
// get the encoding from the Content-Type header 
BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream())); 
String line = null; 
while((line = in.readLine()) != null) { 
    System.out.println(line); 
} 

// close sockets, handle errors, etc. 

के रूप में: आप इसे अपने आप को लागू करने के लिए नहीं है, सबसे अच्छा तरीका है कैसे HttpURLConnection साथ यह करने के लिए का एक उदाहरण है ऐसे जावा एपीआई HttpURLConnection या अपाचे के HttpClient

यहाँ के रूप में मौजूदा कार्यान्वयन उपयोग करने के लिए है ऊपर लिखा गया है, आप स्वीकृति-एन्कोडिंग हेडर जोड़कर यातायात को बचा सकते हैं और प्रतिक्रिया के सामग्री-एन्कोडिंग हेडर को चेक कर सकते हैं।

यहाँ एक HttpClient उदाहरण के लिए, here से लिया है:

// Create an instance of HttpClient. 
    HttpClient client = new HttpClient(); 

    // Create a method instance. 
    GetMethod method = new GetMethod(url); 

    // Provide custom retry handler is necessary 
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
      new DefaultHttpMethodRetryHandler(3, false)); 

    try { 
     // Execute the method. 
     int statusCode = client.executeMethod(method); 

     if (statusCode != HttpStatus.SC_OK) { 
     System.err.println("Method failed: " + method.getStatusLine()); 
     } 

     // Read the response body. 
     byte[] responseBody = method.getResponseBody(); 

     // Deal with the response. 
     // Use caution: ensure correct character encoding and is not binary data 
     System.out.println(new String(responseBody)); 

    } catch (HttpException e) { 
     System.err.println("Fatal protocol violation: " + e.getMessage()); 
     e.printStackTrace(); 
    } catch (IOException e) { 
     System.err.println("Fatal transport error: " + e.getMessage()); 
     e.printStackTrace(); 
    } finally { 
     // Release the connection. 
     method.releaseConnection(); 
    } 
+0

+1। जैसे ही आप एक साधारण जीईटी से परे कुछ भी करना चाहते हैं, यह अमूल्य –

+2

है HttpURLConnection gzipped सामग्री को संभाल नहीं करता है। मैंने वह कठिन रास्ता सीख लिया। –

1

आप तो एक वेबपेज की सामग्री लाने के लिए क्या, आप के बजाय अपने आप को इस कोडिंग के apache httpclient पर एक नज़र रखना चाहिए, प्रयोजनों के सीखने के लिए उम्मीद या कोई अन्य वास्तव में अच्छा कारण है।

0

जावा 8 स्ट्रीम एपीआई का उपयोग कर एक स्ट्रीम से एक स्ट्रिंग बनाने के लिए बहुत आसान:

new BufferedReader(new InputStreamReader(in)).lines().collect(Collectors.joining("\n")) 

इंटेलीजे का उपयोग मैं भी इस beeing एक डिबग अभिव्यक्ति सेट कर सकते हैं: enter image description here

मैं ग्रहण में लगता है कि काम करेंगे यह समान है।

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