Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package gruppe3klient;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.Reader;
- import java.util.ArrayList;
- import java.util.List;
- import org.apache.http.Header;
- import org.apache.http.HeaderElement;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.NameValuePair;
- import org.apache.http.ParseException;
- import org.apache.http.client.CookieStore;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- //import org.apache.http.client.methods.HttpGet;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.client.protocol.ClientContext;
- import org.apache.http.impl.client.BasicCookieStore;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.message.BasicNameValuePair;
- import org.apache.http.util.EntityUtils;
- import org.apache.http.params.HttpProtocolParams;
- import org.apache.http.protocol.BasicHttpContext;
- import org.apache.http.protocol.HTTP;
- import org.apache.http.protocol.HttpContext;
- public class QuickStart {
- public static void main(String[] args) throws Exception {
- DefaultHttpClient httpclient = new DefaultHttpClient();
- // CookieStore eCookie = new BasicCookieStore();//Oppretter CookieStore for å håndtere sessionid
- // HttpContext lager = new BasicHttpContext();
- // lager.setAttribute(ClientContext.COOKIE_STORE,eCookie);
- HttpPost httpPost = new HttpPost("http://oo.hive.no/vlnch");
- List<NameValuePair> nvps = new ArrayList<NameValuePair>();
- nvps.add(new BasicNameValuePair("username", "frasve"));
- nvps.add(new BasicNameValuePair("password", "eksamenbruker"));
- nvps.add(new BasicNameValuePair("request", "login"));
- nvps.add(new BasicNameValuePair("request", "mctestyourself"));
- HttpProtocolParams.setUserAgent(httpclient.getParams(), "android");// Endrer user agent til Android
- httpPost.setEntity(new UrlEncodedFormEntity(nvps));
- HttpResponse response1 = httpclient.execute(httpPost/*,lager*/);
- try {
- Header sessionid = response1.getHeaders("Set-Cookie")[0];
- response1.setHeader("Cookie", "sessionid=" + sessionid);
- //HttpResponse response2 = httpclient.execute(httpPost/*,lager*/);
- System.out.println(response1.getStatusLine());// Får tilbake om
- // tilkobling OK
- System.out.println(HttpProtocolParams.getUserAgent(httpclient
- .getParams()));// Får tilbake user agent
- System.out.println(sessionid);
- HttpEntity entity2 = response1.getEntity();
- System.out.println(_getResponseBody(entity2));
- // System.out.println(entity2.getContent());
- // do something useful with the response body
- // and ensure it is fully consumed
- EntityUtils.consume(entity2);
- } finally {
- httpPost.releaseConnection();
- }
- }
- public static String _getResponseBody(final HttpEntity entity)
- throws IOException, ParseException {
- if (entity == null) {
- throw new IllegalArgumentException("HTTP entity may not be null");
- }
- InputStream instream = entity.getContent();
- if (instream == null) {
- return "";
- }
- if (entity.getContentLength() > Integer.MAX_VALUE) {
- throw new IllegalArgumentException(
- "HTTP entity too large to be buffered in memory");
- }
- String charset = getContentCharSet(entity);
- if (charset == null) {
- charset = HTTP.DEFAULT_CONTENT_CHARSET;
- }
- Reader reader = new InputStreamReader(instream, charset);
- StringBuilder buffer = new StringBuilder();
- try {
- char[] tmp = new char[1024];
- int l;
- while ((l = reader.read(tmp)) != -1) {
- buffer.append(tmp, 0, l);
- }
- } finally {
- reader.close();
- }
- return buffer.toString();
- }
- public static String getContentCharSet(final HttpEntity entity)
- throws ParseException {
- if (entity == null) {
- throw new IllegalArgumentException("HTTP entity may not be null");
- }
- String charset = null;
- if (entity.getContentType() != null) {
- HeaderElement values[] = entity.getContentType().getElements();
- if (values.length > 0) {
- NameValuePair param = values[0].getParameterByName("charset");
- if (param != null) {
- charset = param.getValue();
- }
- }
- }
- return charset;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment