2012-01-29 22 views
6

मैं एक जावा वेब अनुप्रयोग में Apache HTTP ग्राहक (v4) के साथ काम कर रहा हूँ, और मैं निम्नलिखित मामलों में फंस कर रहा हूँ, जिसके लिए मैं की आवश्यकता होती है सरल उपयोग examples--जावा अपाचे HTTP ग्राहक उपयोग कुकीज़ के उपयोग को दिखा और HttpResponse से प्रतिक्रिया आपत्ति निकालने उदाहरण

(1) कुकीज़

के उपयोग के लिए अपाचे HTTP ग्राहक, विभिन्न विकल्पों के साथ कुकीज़ उपलब्ध का उपयोग कैसे करें

(2) निकाला जा रहा है चारसेट, (KeyValuePair के रूप में) माइम प्रकार, प्रतिक्रिया हेडर और budy (के रूप में बाइट []) जब प्रतिक्रिया HTTPResponse ऑब्जेक्ट में उपलब्ध है।

उत्तर

6

1) कुकीज़ के लिए के रूप में, कि exapmle देखें:

httpcomponents-क्लाइंट 4.1.3 \ उदाहरण \ ऑर्ग \ अपाचे \ http \ उदाहरण \ ग्राहक \ ClientCustomContext.java

मुख्य कोड:

HttpClient httpclient = new DefaultHttpClient(); 
     try { 
      // Create a local instance of cookie store 
      CookieStore cookieStore = new BasicCookieStore(); 

      // Create local HTTP context 
      HttpContext localContext = new BasicHttpContext(); 
      // Bind custom cookie store to the local context 
      localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); 

      HttpGet httpget = new HttpGet("http://www.google.com/"); 

      System.out.println("executing request " + httpget.getURI()); 

      // Pass local context as a parameter 
      HttpResponse response = httpclient.execute(httpget, localContext); 
     } finally { 
      // When HttpClient instance is no longer needed, 
      // shut down the connection manager to ensure 
      // immediate deallocation of all system resources 
      httpclient.getConnectionManager().shutdown(); 
     } 

2) आप सभी आप प्रतिक्रिया से की जरूरत है और प्राप्त कर सकते हैं:

HttpEntity entity = response.getEntity(); 
entity.getContent() 

बस में उदाहरण पढ़ें: httpcomponents-क्लाइंट 4.1.3 \ उदाहरण \ ऑर्ग \ अपाचे \ http \ उदाहरण \ httpcomponents-client-4.1.3-bin.zip जिसमें क्लाइंट अपने website से डाउनलोड किया गया।