fhs

Gruppe3Klient.java

fhs
Dec 3rd, 2012
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.20 KB | None | 0 0
  1. package gruppe3klient;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.io.Reader;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9.  
  10. import org.apache.http.Header;
  11. import org.apache.http.HeaderElement;
  12. import org.apache.http.HttpEntity;
  13. import org.apache.http.HttpResponse;
  14. import org.apache.http.NameValuePair;
  15. import org.apache.http.ParseException;
  16. import org.apache.http.client.CookieStore;
  17. import org.apache.http.client.entity.UrlEncodedFormEntity;
  18. //import org.apache.http.client.methods.HttpGet;
  19. import org.apache.http.client.methods.HttpPost;
  20. import org.apache.http.client.protocol.ClientContext;
  21. import org.apache.http.impl.client.BasicCookieStore;
  22. import org.apache.http.impl.client.DefaultHttpClient;
  23. import org.apache.http.message.BasicNameValuePair;
  24. import org.apache.http.util.EntityUtils;
  25. import org.apache.http.params.HttpProtocolParams;
  26. import org.apache.http.protocol.BasicHttpContext;
  27. import org.apache.http.protocol.HTTP;
  28. import org.apache.http.protocol.HttpContext;
  29.  
  30. public class QuickStart {
  31.  
  32.     public static void main(String[] args) throws Exception {
  33.         DefaultHttpClient httpclient = new DefaultHttpClient();
  34.        
  35. //      CookieStore eCookie = new BasicCookieStore();//Oppretter CookieStore for å håndtere sessionid
  36. //      HttpContext lager = new BasicHttpContext();
  37. //      lager.setAttribute(ClientContext.COOKIE_STORE,eCookie);
  38.        
  39.         HttpPost httpPost = new HttpPost("http://oo.hive.no/vlnch");
  40.         List<NameValuePair> nvps = new ArrayList<NameValuePair>();
  41.         nvps.add(new BasicNameValuePair("username", "frasve"));
  42.         nvps.add(new BasicNameValuePair("password", "eksamenbruker"));
  43.         nvps.add(new BasicNameValuePair("request", "login"));
  44.         nvps.add(new BasicNameValuePair("request", "mctestyourself"));
  45.         HttpProtocolParams.setUserAgent(httpclient.getParams(), "android");// Endrer user agent til Android
  46.  
  47.         httpPost.setEntity(new UrlEncodedFormEntity(nvps));
  48.         HttpResponse response1 = httpclient.execute(httpPost/*,lager*/);
  49.        
  50.         try {
  51.             Header sessionid = response1.getHeaders("Set-Cookie")[0];
  52.             response1.setHeader("Cookie", "sessionid=" + sessionid);
  53.            
  54.             //HttpResponse response2 = httpclient.execute(httpPost/*,lager*/);
  55.            
  56.             System.out.println(response1.getStatusLine());// Får tilbake om
  57.                                                             // tilkobling OK
  58.             System.out.println(HttpProtocolParams.getUserAgent(httpclient
  59.                     .getParams()));// Får tilbake user agent
  60.             System.out.println(sessionid);
  61.  
  62.             HttpEntity entity2 = response1.getEntity();
  63.             System.out.println(_getResponseBody(entity2));
  64.  
  65.             // System.out.println(entity2.getContent());
  66.            
  67.             // do something useful with the response body
  68.             // and ensure it is fully consumed
  69.             EntityUtils.consume(entity2);
  70.         } finally {
  71.             httpPost.releaseConnection();
  72.         }
  73.     }
  74.  
  75.     public static String _getResponseBody(final HttpEntity entity)
  76.             throws IOException, ParseException {
  77.         if (entity == null) {
  78.             throw new IllegalArgumentException("HTTP entity may not be null");
  79.         }
  80.         InputStream instream = entity.getContent();
  81.         if (instream == null) {
  82.             return "";
  83.         }
  84.         if (entity.getContentLength() > Integer.MAX_VALUE) {
  85.             throw new IllegalArgumentException(
  86.                     "HTTP entity too large to be buffered in memory");
  87.         }
  88.         String charset = getContentCharSet(entity);
  89.         if (charset == null) {
  90.             charset = HTTP.DEFAULT_CONTENT_CHARSET;
  91.         }
  92.         Reader reader = new InputStreamReader(instream, charset);
  93.         StringBuilder buffer = new StringBuilder();
  94.         try {
  95.             char[] tmp = new char[1024];
  96.             int l;
  97.             while ((l = reader.read(tmp)) != -1) {
  98.                 buffer.append(tmp, 0, l);
  99.             }
  100.         } finally {
  101.             reader.close();
  102.         }
  103.         return buffer.toString();
  104.     }
  105.  
  106.     public static String getContentCharSet(final HttpEntity entity)
  107.             throws ParseException {
  108.  
  109.         if (entity == null) {
  110.             throw new IllegalArgumentException("HTTP entity may not be null");
  111.         }
  112.  
  113.         String charset = null;
  114.  
  115.         if (entity.getContentType() != null) {
  116.  
  117.             HeaderElement values[] = entity.getContentType().getElements();
  118.  
  119.             if (values.length > 0) {
  120.  
  121.                 NameValuePair param = values[0].getParameterByName("charset");
  122.  
  123.                 if (param != null) {
  124.  
  125.                     charset = param.getValue();
  126.  
  127.                 }
  128.  
  129.             }
  130.  
  131.         }
  132.  
  133.         return charset;
  134.  
  135.     }
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment