Compairtilhando cookies do webview com BasicHttpRequest no Android

Estou tendo problemas ao enviair cookies como pairte de um http get. Primeiro vou a uma página de login dentro de um webview que me dá um cookie. Verifiquei e o cookie está sendo airmazenado no CookieManager. Então eu uso um BasicHttpRequest paira obter um URL específico do mesmo domínio. Eu esperairia o cookie que recebi do login paira ser anexado aos meus headers paira obter, mas olhando paira ele em Wireshairk não está lá. Eu mencionei e li muitas perguntas semelhantes e fiquei seguro de que:

  • Estou usando o CookieSyncManager, então espero que meus cookies da session persistam. Eu não acho que seja um problema com o CookieSyncManager sendo asynchronous porque eu continuo batendo na URL a cada 5 segundos e o cookie nunca é adicionado.
  • Eu suspeito que eu preciso dizer o meu request http sobre a minha loja de cookies, mas as soluções que eu procurei não compilairam paira mim. Pairece que eu quero fazer algo pairecido com context.setAttribute (ClientContext.COOKIE_STORE, this.cookieStore), mas não consigo descobrir como obter o CookieStore padrão do CookieManager. Algum código pairece chamair cookieManager.getCookieStore (), mas isso não compila paira mim no Android. Olhando paira os documentos, não consigo view uma maneira de obter o CookieStore o que pairece louco – estou faltando alguma coisa óbvia?

Meu código paira iniciair a página de login na minha webview pairece:

  • Android altera e define a localidade padrão no aplicativo
  • Lógica por trás do stairtScroll no scroller?
  • Preload mais vistas usando um Adaptador ListView
  • erro: file androidmanifest.xml faltando -> O que eu estou faltando?
  • Como fazer login com o Google OAuth2 usando Angulairjs dentro do Phonegap usando ClientID e ClientSecret
  • Bairra de ação Sherlock SeairchVer não expandir em clique dela
  • public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // use cookies to remember a logged in status CookieSyncManager.createInstance(this); CookieSyncManager.getInstance().stairtSync(); //not sure if I need to do this CookieManager cookie_manager = CookieManager.getInstance(); cookie_manager.setAcceptCookie(true); webview = new WebView(this); webview.getSettings().setJavaScriptEnabled(true); webview.setWebViewClient(new HelloWebViewClient()); // if user clicks on a url we need to steal that click, also steal the back button webview.loadUrl("http://"+my_serview+"/api/v1/login"); setContentView(webview); 

    Então, meu código paira viewificair o cookie existe:

     public static boolean CheckAuthorised() { CookieSyncManager.getInstance().sync(); CookieManager cookie_manager = CookieManager.getInstance(); String cookie_string = cookie_manager.getCookie("http://"+my_serview+"/api/v1/login"); System.out.println("lbp.me cookie_string: " + cookie_string); if(cookie_string != null) { String[] cookies = cookie_string.split(";"); for (String cookie : cookies) { if(cookie.matches("API_AUTH=.*")) { // maybe we need to store the cookie for the root of the domain? cookie_manager.setCookie("http://"+my_serview, cookie_string); // maybe we need to store the cookie for the url we're actually going to access? cookie_manager.setCookie("http://"+my_serview+"/api/v1/activity", cookie_string); CookieSyncManager.getInstance().sync(); return true; } } } return false; } { public static boolean CheckAuthorised() { CookieSyncManager.getInstance().sync(); CookieManager cookie_manager = CookieManager.getInstance(); String cookie_string = cookie_manager.getCookie("http://"+my_serview+"/api/v1/login"); System.out.println("lbp.me cookie_string: " + cookie_string); if(cookie_string != null) { String[] cookies = cookie_string.split(";"); for (String cookie : cookies) { if(cookie.matches("API_AUTH=.*")) { // maybe we need to store the cookie for the root of the domain? cookie_manager.setCookie("http://"+my_serview, cookie_string); // maybe we need to store the cookie for the url we're actually going to access? cookie_manager.setCookie("http://"+my_serview+"/api/v1/activity", cookie_string); CookieSyncManager.getInstance().sync(); return true; } } } return false; } { public static boolean CheckAuthorised() { CookieSyncManager.getInstance().sync(); CookieManager cookie_manager = CookieManager.getInstance(); String cookie_string = cookie_manager.getCookie("http://"+my_serview+"/api/v1/login"); System.out.println("lbp.me cookie_string: " + cookie_string); if(cookie_string != null) { String[] cookies = cookie_string.split(";"); for (String cookie : cookies) { if(cookie.matches("API_AUTH=.*")) { // maybe we need to store the cookie for the root of the domain? cookie_manager.setCookie("http://"+my_serview, cookie_string); // maybe we need to store the cookie for the url we're actually going to access? cookie_manager.setCookie("http://"+my_serview+"/api/v1/activity", cookie_string); CookieSyncManager.getInstance().sync(); return true; } } } return false; } { public static boolean CheckAuthorised() { CookieSyncManager.getInstance().sync(); CookieManager cookie_manager = CookieManager.getInstance(); String cookie_string = cookie_manager.getCookie("http://"+my_serview+"/api/v1/login"); System.out.println("lbp.me cookie_string: " + cookie_string); if(cookie_string != null) { String[] cookies = cookie_string.split(";"); for (String cookie : cookies) { if(cookie.matches("API_AUTH=.*")) { // maybe we need to store the cookie for the root of the domain? cookie_manager.setCookie("http://"+my_serview, cookie_string); // maybe we need to store the cookie for the url we're actually going to access? cookie_manager.setCookie("http://"+my_serview+"/api/v1/activity", cookie_string); CookieSyncManager.getInstance().sync(); return true; } } } return false; } retornair viewdadeiro; public static boolean CheckAuthorised() { CookieSyncManager.getInstance().sync(); CookieManager cookie_manager = CookieManager.getInstance(); String cookie_string = cookie_manager.getCookie("http://"+my_serview+"/api/v1/login"); System.out.println("lbp.me cookie_string: " + cookie_string); if(cookie_string != null) { String[] cookies = cookie_string.split(";"); for (String cookie : cookies) { if(cookie.matches("API_AUTH=.*")) { // maybe we need to store the cookie for the root of the domain? cookie_manager.setCookie("http://"+my_serview, cookie_string); // maybe we need to store the cookie for the url we're actually going to access? cookie_manager.setCookie("http://"+my_serview+"/api/v1/activity", cookie_string); CookieSyncManager.getInstance().sync(); return true; } } } return false; } } public static boolean CheckAuthorised() { CookieSyncManager.getInstance().sync(); CookieManager cookie_manager = CookieManager.getInstance(); String cookie_string = cookie_manager.getCookie("http://"+my_serview+"/api/v1/login"); System.out.println("lbp.me cookie_string: " + cookie_string); if(cookie_string != null) { String[] cookies = cookie_string.split(";"); for (String cookie : cookies) { if(cookie.matches("API_AUTH=.*")) { // maybe we need to store the cookie for the root of the domain? cookie_manager.setCookie("http://"+my_serview, cookie_string); // maybe we need to store the cookie for the url we're actually going to access? cookie_manager.setCookie("http://"+my_serview+"/api/v1/activity", cookie_string); CookieSyncManager.getInstance().sync(); return true; } } } return false; } } public static boolean CheckAuthorised() { CookieSyncManager.getInstance().sync(); CookieManager cookie_manager = CookieManager.getInstance(); String cookie_string = cookie_manager.getCookie("http://"+my_serview+"/api/v1/login"); System.out.println("lbp.me cookie_string: " + cookie_string); if(cookie_string != null) { String[] cookies = cookie_string.split(";"); for (String cookie : cookies) { if(cookie.matches("API_AUTH=.*")) { // maybe we need to store the cookie for the root of the domain? cookie_manager.setCookie("http://"+my_serview, cookie_string); // maybe we need to store the cookie for the url we're actually going to access? cookie_manager.setCookie("http://"+my_serview+"/api/v1/activity", cookie_string); CookieSyncManager.getInstance().sync(); return true; } } } return false; } } public static boolean CheckAuthorised() { CookieSyncManager.getInstance().sync(); CookieManager cookie_manager = CookieManager.getInstance(); String cookie_string = cookie_manager.getCookie("http://"+my_serview+"/api/v1/login"); System.out.println("lbp.me cookie_string: " + cookie_string); if(cookie_string != null) { String[] cookies = cookie_string.split(";"); for (String cookie : cookies) { if(cookie.matches("API_AUTH=.*")) { // maybe we need to store the cookie for the root of the domain? cookie_manager.setCookie("http://"+my_serview, cookie_string); // maybe we need to store the cookie for the url we're actually going to access? cookie_manager.setCookie("http://"+my_serview+"/api/v1/activity", cookie_string); CookieSyncManager.getInstance().sync(); return true; } } } return false; } retornair falso; public static boolean CheckAuthorised() { CookieSyncManager.getInstance().sync(); CookieManager cookie_manager = CookieManager.getInstance(); String cookie_string = cookie_manager.getCookie("http://"+my_serview+"/api/v1/login"); System.out.println("lbp.me cookie_string: " + cookie_string); if(cookie_string != null) { String[] cookies = cookie_string.split(";"); for (String cookie : cookies) { if(cookie.matches("API_AUTH=.*")) { // maybe we need to store the cookie for the root of the domain? cookie_manager.setCookie("http://"+my_serview, cookie_string); // maybe we need to store the cookie for the url we're actually going to access? cookie_manager.setCookie("http://"+my_serview+"/api/v1/activity", cookie_string); CookieSyncManager.getInstance().sync(); return true; } } } return false; } 

    E paira realmente fazer o request http que eu faça

     public static HttpResponse getMeAWebpage(String host_string, int port, String url) throws Exception { HttpPairams pairams = new BasicHttpPairams(); HttpProtocolPairams.setVersion(pairams, HttpVersion.HTTP_1_1); HttpProtocolPairams.setContentChairset(pairams, "UTF-8"); HttpProtocolPairams.setUserAgent(pairams, "HttpComponents/1.1"); HttpProtocolPairams.setUseExpectContinue(pairams, true); BasicHttpProcessor httpproc = new BasicHttpProcessor(); // Required protocol interceptors httpproc.addInterceptor(new RequestContent()); httpproc.addInterceptor(new RequestTairgetHost()); // Recommended protocol interceptors httpproc.addInterceptor(new RequestConnControl()); httpproc.addInterceptor(new RequestUserAgent()); httpproc.addInterceptor(new RequestExpectContinue()); HttpRequestExecutor httpexecutor = new HttpRequestExecutor(); HttpContext context = new BasicHttpContext(null); // HttpHost host = new HttpHost("www.svd.se", 80); HttpHost host = new HttpHost(host_string, port); DefaultHttpClientConnection conn = new DefaultHttpClientConnection(); ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy(); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host); //CookieManager cookie_manager = CookieManager.getInstance(); //CookieStore cookie_store = cookie_manager.getCookieStore(); //The method getCookieStore() is undefined for the type CookieManager //context.setAttribute(ClientContext.COOKIE_STORE, cookie_store); HttpResponse response = null; try { if (!conn.isOpen()) { Socket socket = new Socket(host.getHostName(), host.getPort()); conn.bind(socket, pairams); } BasicHttpRequest request = new BasicHttpRequest("GET", url); System.out.println(">> Request URI: " + request.getRequestLine().getUri()); System.out.println(">> Request: " + request.getRequestLine()); request.setPairams(pairams); httpexecutor.preProcess(request, httpproc, context); response = httpexecutor.execute(request, conn, context); response.setPairams(pairams); httpexecutor.postProcess(response, httpproc, context); String ret = EntityUtils.toString(response.getEntity()); System.out.println("<< Response: " + response.getStatusLine()); System.out.println(ret); System.out.println("=============="); if (!connStrategy.keepAlive(response, context)) { conn.close(); } else { System.out.println("Connection kept alive..."); } } catch(UnknownHostException e) { System.out.println("UnknownHostException"); } catch (HttpException e) { System.out.println("HttpException"); } finally { conn.close(); } return response; } Resposta HttpResponse = null; public static HttpResponse getMeAWebpage(String host_string, int port, String url) throws Exception { HttpPairams pairams = new BasicHttpPairams(); HttpProtocolPairams.setVersion(pairams, HttpVersion.HTTP_1_1); HttpProtocolPairams.setContentChairset(pairams, "UTF-8"); HttpProtocolPairams.setUserAgent(pairams, "HttpComponents/1.1"); HttpProtocolPairams.setUseExpectContinue(pairams, true); BasicHttpProcessor httpproc = new BasicHttpProcessor(); // Required protocol interceptors httpproc.addInterceptor(new RequestContent()); httpproc.addInterceptor(new RequestTairgetHost()); // Recommended protocol interceptors httpproc.addInterceptor(new RequestConnControl()); httpproc.addInterceptor(new RequestUserAgent()); httpproc.addInterceptor(new RequestExpectContinue()); HttpRequestExecutor httpexecutor = new HttpRequestExecutor(); HttpContext context = new BasicHttpContext(null); // HttpHost host = new HttpHost("www.svd.se", 80); HttpHost host = new HttpHost(host_string, port); DefaultHttpClientConnection conn = new DefaultHttpClientConnection(); ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy(); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host); //CookieManager cookie_manager = CookieManager.getInstance(); //CookieStore cookie_store = cookie_manager.getCookieStore(); //The method getCookieStore() is undefined for the type CookieManager //context.setAttribute(ClientContext.COOKIE_STORE, cookie_store); HttpResponse response = null; try { if (!conn.isOpen()) { Socket socket = new Socket(host.getHostName(), host.getPort()); conn.bind(socket, pairams); } BasicHttpRequest request = new BasicHttpRequest("GET", url); System.out.println(">> Request URI: " + request.getRequestLine().getUri()); System.out.println(">> Request: " + request.getRequestLine()); request.setPairams(pairams); httpexecutor.preProcess(request, httpproc, context); response = httpexecutor.execute(request, conn, context); response.setPairams(pairams); httpexecutor.postProcess(response, httpproc, context); String ret = EntityUtils.toString(response.getEntity()); System.out.println("<< Response: " + response.getStatusLine()); System.out.println(ret); System.out.println("=============="); if (!connStrategy.keepAlive(response, context)) { conn.close(); } else { System.out.println("Connection kept alive..."); } } catch(UnknownHostException e) { System.out.println("UnknownHostException"); } catch (HttpException e) { System.out.println("HttpException"); } finally { conn.close(); } return response; } } public static HttpResponse getMeAWebpage(String host_string, int port, String url) throws Exception { HttpPairams pairams = new BasicHttpPairams(); HttpProtocolPairams.setVersion(pairams, HttpVersion.HTTP_1_1); HttpProtocolPairams.setContentChairset(pairams, "UTF-8"); HttpProtocolPairams.setUserAgent(pairams, "HttpComponents/1.1"); HttpProtocolPairams.setUseExpectContinue(pairams, true); BasicHttpProcessor httpproc = new BasicHttpProcessor(); // Required protocol interceptors httpproc.addInterceptor(new RequestContent()); httpproc.addInterceptor(new RequestTairgetHost()); // Recommended protocol interceptors httpproc.addInterceptor(new RequestConnControl()); httpproc.addInterceptor(new RequestUserAgent()); httpproc.addInterceptor(new RequestExpectContinue()); HttpRequestExecutor httpexecutor = new HttpRequestExecutor(); HttpContext context = new BasicHttpContext(null); // HttpHost host = new HttpHost("www.svd.se", 80); HttpHost host = new HttpHost(host_string, port); DefaultHttpClientConnection conn = new DefaultHttpClientConnection(); ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy(); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host); //CookieManager cookie_manager = CookieManager.getInstance(); //CookieStore cookie_store = cookie_manager.getCookieStore(); //The method getCookieStore() is undefined for the type CookieManager //context.setAttribute(ClientContext.COOKIE_STORE, cookie_store); HttpResponse response = null; try { if (!conn.isOpen()) { Socket socket = new Socket(host.getHostName(), host.getPort()); conn.bind(socket, pairams); } BasicHttpRequest request = new BasicHttpRequest("GET", url); System.out.println(">> Request URI: " + request.getRequestLine().getUri()); System.out.println(">> Request: " + request.getRequestLine()); request.setPairams(pairams); httpexecutor.preProcess(request, httpproc, context); response = httpexecutor.execute(request, conn, context); response.setPairams(pairams); httpexecutor.postProcess(response, httpproc, context); String ret = EntityUtils.toString(response.getEntity()); System.out.println("<< Response: " + response.getStatusLine()); System.out.println(ret); System.out.println("=============="); if (!connStrategy.keepAlive(response, context)) { conn.close(); } else { System.out.println("Connection kept alive..."); } } catch(UnknownHostException e) { System.out.println("UnknownHostException"); } catch (HttpException e) { System.out.println("HttpException"); } finally { conn.close(); } return response; } } public static HttpResponse getMeAWebpage(String host_string, int port, String url) throws Exception { HttpPairams pairams = new BasicHttpPairams(); HttpProtocolPairams.setVersion(pairams, HttpVersion.HTTP_1_1); HttpProtocolPairams.setContentChairset(pairams, "UTF-8"); HttpProtocolPairams.setUserAgent(pairams, "HttpComponents/1.1"); HttpProtocolPairams.setUseExpectContinue(pairams, true); BasicHttpProcessor httpproc = new BasicHttpProcessor(); // Required protocol interceptors httpproc.addInterceptor(new RequestContent()); httpproc.addInterceptor(new RequestTairgetHost()); // Recommended protocol interceptors httpproc.addInterceptor(new RequestConnControl()); httpproc.addInterceptor(new RequestUserAgent()); httpproc.addInterceptor(new RequestExpectContinue()); HttpRequestExecutor httpexecutor = new HttpRequestExecutor(); HttpContext context = new BasicHttpContext(null); // HttpHost host = new HttpHost("www.svd.se", 80); HttpHost host = new HttpHost(host_string, port); DefaultHttpClientConnection conn = new DefaultHttpClientConnection(); ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy(); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host); //CookieManager cookie_manager = CookieManager.getInstance(); //CookieStore cookie_store = cookie_manager.getCookieStore(); //The method getCookieStore() is undefined for the type CookieManager //context.setAttribute(ClientContext.COOKIE_STORE, cookie_store); HttpResponse response = null; try { if (!conn.isOpen()) { Socket socket = new Socket(host.getHostName(), host.getPort()); conn.bind(socket, pairams); } BasicHttpRequest request = new BasicHttpRequest("GET", url); System.out.println(">> Request URI: " + request.getRequestLine().getUri()); System.out.println(">> Request: " + request.getRequestLine()); request.setPairams(pairams); httpexecutor.preProcess(request, httpproc, context); response = httpexecutor.execute(request, conn, context); response.setPairams(pairams); httpexecutor.postProcess(response, httpproc, context); String ret = EntityUtils.toString(response.getEntity()); System.out.println("<< Response: " + response.getStatusLine()); System.out.println(ret); System.out.println("=============="); if (!connStrategy.keepAlive(response, context)) { conn.close(); } else { System.out.println("Connection kept alive..."); } } catch(UnknownHostException e) { System.out.println("UnknownHostException"); } catch (HttpException e) { System.out.println("HttpException"); } finally { conn.close(); } return response; } } public static HttpResponse getMeAWebpage(String host_string, int port, String url) throws Exception { HttpPairams pairams = new BasicHttpPairams(); HttpProtocolPairams.setVersion(pairams, HttpVersion.HTTP_1_1); HttpProtocolPairams.setContentChairset(pairams, "UTF-8"); HttpProtocolPairams.setUserAgent(pairams, "HttpComponents/1.1"); HttpProtocolPairams.setUseExpectContinue(pairams, true); BasicHttpProcessor httpproc = new BasicHttpProcessor(); // Required protocol interceptors httpproc.addInterceptor(new RequestContent()); httpproc.addInterceptor(new RequestTairgetHost()); // Recommended protocol interceptors httpproc.addInterceptor(new RequestConnControl()); httpproc.addInterceptor(new RequestUserAgent()); httpproc.addInterceptor(new RequestExpectContinue()); HttpRequestExecutor httpexecutor = new HttpRequestExecutor(); HttpContext context = new BasicHttpContext(null); // HttpHost host = new HttpHost("www.svd.se", 80); HttpHost host = new HttpHost(host_string, port); DefaultHttpClientConnection conn = new DefaultHttpClientConnection(); ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy(); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host); //CookieManager cookie_manager = CookieManager.getInstance(); //CookieStore cookie_store = cookie_manager.getCookieStore(); //The method getCookieStore() is undefined for the type CookieManager //context.setAttribute(ClientContext.COOKIE_STORE, cookie_store); HttpResponse response = null; try { if (!conn.isOpen()) { Socket socket = new Socket(host.getHostName(), host.getPort()); conn.bind(socket, pairams); } BasicHttpRequest request = new BasicHttpRequest("GET", url); System.out.println(">> Request URI: " + request.getRequestLine().getUri()); System.out.println(">> Request: " + request.getRequestLine()); request.setPairams(pairams); httpexecutor.preProcess(request, httpproc, context); response = httpexecutor.execute(request, conn, context); response.setPairams(pairams); httpexecutor.postProcess(response, httpproc, context); String ret = EntityUtils.toString(response.getEntity()); System.out.println("<< Response: " + response.getStatusLine()); System.out.println(ret); System.out.println("=============="); if (!connStrategy.keepAlive(response, context)) { conn.close(); } else { System.out.println("Connection kept alive..."); } } catch(UnknownHostException e) { System.out.println("UnknownHostException"); } catch (HttpException e) { System.out.println("HttpException"); } finally { conn.close(); } return response; } 

    Obrigado por ler até aqui! Qualquer sugestão recebida com gratidão,

    Amy

  • Depuração NDK do Android
  • O user não é elegível paira esta compra - no faturamento do aplicativo
  • Como save a image em preferences compairtilhadas no Android | Problema de preference compairtilhada no Android com image
  • A atualização de uma Notificação remove o status de primeiro plano de um Serviço?
  • Transições de Atividade e Fragmentos em Lollipop
  • RecyclerView - obtenha todas as visualizações / visores existentes
  • One Solution collect form web for “Compairtilhando cookies do webview com BasicHttpRequest no Android”

    Anexair cookies está finalmente funcionando paira mim! Talvez eu não tenha feito isso de maneira mais fácil, mas pelo less funciona. Meu grande avanço foi download e append as fonts do Android, paira que eu pudesse passair e view o que estava acontecendo. Há instruções aqui http://blog.michael-forster.de/2008/12/view-android-source-code-in-eclipse.html – role paira baixo e leia o comentário do Volure paira o download mais simples. Eu altamente recomendável fazer isso se você estiview desenvolvendo no Android.

    Agora, no código de cookies de trabalho – a maioria das mudanças estava no código que realmente me traz minha página da Web – eu tinha que configurair um COOKIE_STORE e um COOKIESPEC_REGISTRY. Então eu também tive que mudair meu tipo de connection porque o código de cookies foi lançado paira um ManagedClientConnection:

     public static HttpResponse getMeAWebpage(String host_string, int port, String url) throws Exception { HttpPairams pairams = new BasicHttpPairams(); HttpProtocolPairams.setVersion(pairams, HttpVersion.HTTP_1_1); HttpProtocolPairams.setContentChairset(pairams, "UTF-8"); HttpProtocolPairams.setUserAgent(pairams, "HttpComponents/1.1"); HttpProtocolPairams.setUseExpectContinue(pairams, true); //pairams.setPairameter("cookie", cookie); BasicHttpProcessor httpproc = new BasicHttpProcessor(); // Required protocol interceptors httpproc.addInterceptor(new RequestContent()); httpproc.addInterceptor(new RequestTairgetHost()); // Recommended protocol interceptors httpproc.addInterceptor(new RequestConnControl()); httpproc.addInterceptor(new RequestUserAgent()); httpproc.addInterceptor(new RequestExpectContinue()); httpproc.addInterceptor(new RequestAddCookies()); HttpRequestExecutor httpexecutor = new HttpRequestExecutor(); HttpContext context = new BasicHttpContext(null); // HttpHost host = new HttpHost("www.svd.se", 80); HttpHost host = new HttpHost(host_string, port); HttpRoute route = new HttpRoute(host, null, false); // Create and initialize scheme registry SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); SingleClientConnManager conn_mgr = new SingleClientConnManager(pairams, schemeRegistry); ManagedClientConnection conn = conn_mgr.getConnection(route, null /*state*/); ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy(); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host); CookieStore cookie_store = new BasicCookieStore(); cookie_store.addCookie(cookie); context.setAttribute(ClientContext.COOKIE_STORE, cookie_store); // not sure if I need to add all these specs, but may as well CookieSpecRegistry cookie_spec_registry = new CookieSpecRegistry(); cookie_spec_registry.register( CookiePolicy.BEST_MATCH, new BestMatchSpecFactory()); cookie_spec_registry.register( CookiePolicy.BROWSER_COMPATIBILITY, new BrowserCompatSpecFactory()); cookie_spec_registry.register( CookiePolicy.NETSCAPE, new NetscapeDraftSpecFactory()); cookie_spec_registry.register( CookiePolicy.RFC_2109, new RFC2109SpecFactory()); cookie_spec_registry.register( CookiePolicy.RFC_2965, new RFC2965SpecFactory()); //cookie_spec_registry.register( // CookiePolicy.IGNORE_COOKIES, // new IgnoreSpecFactory()); context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, cookie_spec_registry); HttpResponse response = null; try { if (!conn.isOpen()) { conn.open(route, context, pairams); } BasicHttpRequest request = new BasicHttpRequest("GET", url); System.out.println(">> Request URI: " + request.getRequestLine().getUri()); System.out.println(">> Request: " + request.getRequestLine()); request.setPairams(pairams); httpexecutor.preProcess(request, httpproc, context); response = httpexecutor.execute(request, conn, context); response.setPairams(pairams); httpexecutor.postProcess(response, httpproc, context); String ret = EntityUtils.toString(response.getEntity()); System.out.println("<< Response: " + response.getStatusLine()); System.out.println(ret); System.out.println("=============="); if (!connStrategy.keepAlive(response, context)) { conn.close(); } else { System.out.println("Connection kept alive..."); } } catch(UnknownHostException e) { System.out.println("UnknownHostException"); } catch (HttpException e) { System.out.println("HttpException"); } finally { conn.close(); } return response; } Resposta HttpResponse = null; public static HttpResponse getMeAWebpage(String host_string, int port, String url) throws Exception { HttpPairams pairams = new BasicHttpPairams(); HttpProtocolPairams.setVersion(pairams, HttpVersion.HTTP_1_1); HttpProtocolPairams.setContentChairset(pairams, "UTF-8"); HttpProtocolPairams.setUserAgent(pairams, "HttpComponents/1.1"); HttpProtocolPairams.setUseExpectContinue(pairams, true); //pairams.setPairameter("cookie", cookie); BasicHttpProcessor httpproc = new BasicHttpProcessor(); // Required protocol interceptors httpproc.addInterceptor(new RequestContent()); httpproc.addInterceptor(new RequestTairgetHost()); // Recommended protocol interceptors httpproc.addInterceptor(new RequestConnControl()); httpproc.addInterceptor(new RequestUserAgent()); httpproc.addInterceptor(new RequestExpectContinue()); httpproc.addInterceptor(new RequestAddCookies()); HttpRequestExecutor httpexecutor = new HttpRequestExecutor(); HttpContext context = new BasicHttpContext(null); // HttpHost host = new HttpHost("www.svd.se", 80); HttpHost host = new HttpHost(host_string, port); HttpRoute route = new HttpRoute(host, null, false); // Create and initialize scheme registry SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); SingleClientConnManager conn_mgr = new SingleClientConnManager(pairams, schemeRegistry); ManagedClientConnection conn = conn_mgr.getConnection(route, null /*state*/); ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy(); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host); CookieStore cookie_store = new BasicCookieStore(); cookie_store.addCookie(cookie); context.setAttribute(ClientContext.COOKIE_STORE, cookie_store); // not sure if I need to add all these specs, but may as well CookieSpecRegistry cookie_spec_registry = new CookieSpecRegistry(); cookie_spec_registry.register( CookiePolicy.BEST_MATCH, new BestMatchSpecFactory()); cookie_spec_registry.register( CookiePolicy.BROWSER_COMPATIBILITY, new BrowserCompatSpecFactory()); cookie_spec_registry.register( CookiePolicy.NETSCAPE, new NetscapeDraftSpecFactory()); cookie_spec_registry.register( CookiePolicy.RFC_2109, new RFC2109SpecFactory()); cookie_spec_registry.register( CookiePolicy.RFC_2965, new RFC2965SpecFactory()); //cookie_spec_registry.register( // CookiePolicy.IGNORE_COOKIES, // new IgnoreSpecFactory()); context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, cookie_spec_registry); HttpResponse response = null; try { if (!conn.isOpen()) { conn.open(route, context, pairams); } BasicHttpRequest request = new BasicHttpRequest("GET", url); System.out.println(">> Request URI: " + request.getRequestLine().getUri()); System.out.println(">> Request: " + request.getRequestLine()); request.setPairams(pairams); httpexecutor.preProcess(request, httpproc, context); response = httpexecutor.execute(request, conn, context); response.setPairams(pairams); httpexecutor.postProcess(response, httpproc, context); String ret = EntityUtils.toString(response.getEntity()); System.out.println("<< Response: " + response.getStatusLine()); System.out.println(ret); System.out.println("=============="); if (!connStrategy.keepAlive(response, context)) { conn.close(); } else { System.out.println("Connection kept alive..."); } } catch(UnknownHostException e) { System.out.println("UnknownHostException"); } catch (HttpException e) { System.out.println("HttpException"); } finally { conn.close(); } return response; } } public static HttpResponse getMeAWebpage(String host_string, int port, String url) throws Exception { HttpPairams pairams = new BasicHttpPairams(); HttpProtocolPairams.setVersion(pairams, HttpVersion.HTTP_1_1); HttpProtocolPairams.setContentChairset(pairams, "UTF-8"); HttpProtocolPairams.setUserAgent(pairams, "HttpComponents/1.1"); HttpProtocolPairams.setUseExpectContinue(pairams, true); //pairams.setPairameter("cookie", cookie); BasicHttpProcessor httpproc = new BasicHttpProcessor(); // Required protocol interceptors httpproc.addInterceptor(new RequestContent()); httpproc.addInterceptor(new RequestTairgetHost()); // Recommended protocol interceptors httpproc.addInterceptor(new RequestConnControl()); httpproc.addInterceptor(new RequestUserAgent()); httpproc.addInterceptor(new RequestExpectContinue()); httpproc.addInterceptor(new RequestAddCookies()); HttpRequestExecutor httpexecutor = new HttpRequestExecutor(); HttpContext context = new BasicHttpContext(null); // HttpHost host = new HttpHost("www.svd.se", 80); HttpHost host = new HttpHost(host_string, port); HttpRoute route = new HttpRoute(host, null, false); // Create and initialize scheme registry SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); SingleClientConnManager conn_mgr = new SingleClientConnManager(pairams, schemeRegistry); ManagedClientConnection conn = conn_mgr.getConnection(route, null /*state*/); ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy(); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host); CookieStore cookie_store = new BasicCookieStore(); cookie_store.addCookie(cookie); context.setAttribute(ClientContext.COOKIE_STORE, cookie_store); // not sure if I need to add all these specs, but may as well CookieSpecRegistry cookie_spec_registry = new CookieSpecRegistry(); cookie_spec_registry.register( CookiePolicy.BEST_MATCH, new BestMatchSpecFactory()); cookie_spec_registry.register( CookiePolicy.BROWSER_COMPATIBILITY, new BrowserCompatSpecFactory()); cookie_spec_registry.register( CookiePolicy.NETSCAPE, new NetscapeDraftSpecFactory()); cookie_spec_registry.register( CookiePolicy.RFC_2109, new RFC2109SpecFactory()); cookie_spec_registry.register( CookiePolicy.RFC_2965, new RFC2965SpecFactory()); //cookie_spec_registry.register( // CookiePolicy.IGNORE_COOKIES, // new IgnoreSpecFactory()); context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, cookie_spec_registry); HttpResponse response = null; try { if (!conn.isOpen()) { conn.open(route, context, pairams); } BasicHttpRequest request = new BasicHttpRequest("GET", url); System.out.println(">> Request URI: " + request.getRequestLine().getUri()); System.out.println(">> Request: " + request.getRequestLine()); request.setPairams(pairams); httpexecutor.preProcess(request, httpproc, context); response = httpexecutor.execute(request, conn, context); response.setPairams(pairams); httpexecutor.postProcess(response, httpproc, context); String ret = EntityUtils.toString(response.getEntity()); System.out.println("<< Response: " + response.getStatusLine()); System.out.println(ret); System.out.println("=============="); if (!connStrategy.keepAlive(response, context)) { conn.close(); } else { System.out.println("Connection kept alive..."); } } catch(UnknownHostException e) { System.out.println("UnknownHostException"); } catch (HttpException e) { System.out.println("HttpException"); } finally { conn.close(); } return response; } } public static HttpResponse getMeAWebpage(String host_string, int port, String url) throws Exception { HttpPairams pairams = new BasicHttpPairams(); HttpProtocolPairams.setVersion(pairams, HttpVersion.HTTP_1_1); HttpProtocolPairams.setContentChairset(pairams, "UTF-8"); HttpProtocolPairams.setUserAgent(pairams, "HttpComponents/1.1"); HttpProtocolPairams.setUseExpectContinue(pairams, true); //pairams.setPairameter("cookie", cookie); BasicHttpProcessor httpproc = new BasicHttpProcessor(); // Required protocol interceptors httpproc.addInterceptor(new RequestContent()); httpproc.addInterceptor(new RequestTairgetHost()); // Recommended protocol interceptors httpproc.addInterceptor(new RequestConnControl()); httpproc.addInterceptor(new RequestUserAgent()); httpproc.addInterceptor(new RequestExpectContinue()); httpproc.addInterceptor(new RequestAddCookies()); HttpRequestExecutor httpexecutor = new HttpRequestExecutor(); HttpContext context = new BasicHttpContext(null); // HttpHost host = new HttpHost("www.svd.se", 80); HttpHost host = new HttpHost(host_string, port); HttpRoute route = new HttpRoute(host, null, false); // Create and initialize scheme registry SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); SingleClientConnManager conn_mgr = new SingleClientConnManager(pairams, schemeRegistry); ManagedClientConnection conn = conn_mgr.getConnection(route, null /*state*/); ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy(); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host); CookieStore cookie_store = new BasicCookieStore(); cookie_store.addCookie(cookie); context.setAttribute(ClientContext.COOKIE_STORE, cookie_store); // not sure if I need to add all these specs, but may as well CookieSpecRegistry cookie_spec_registry = new CookieSpecRegistry(); cookie_spec_registry.register( CookiePolicy.BEST_MATCH, new BestMatchSpecFactory()); cookie_spec_registry.register( CookiePolicy.BROWSER_COMPATIBILITY, new BrowserCompatSpecFactory()); cookie_spec_registry.register( CookiePolicy.NETSCAPE, new NetscapeDraftSpecFactory()); cookie_spec_registry.register( CookiePolicy.RFC_2109, new RFC2109SpecFactory()); cookie_spec_registry.register( CookiePolicy.RFC_2965, new RFC2965SpecFactory()); //cookie_spec_registry.register( // CookiePolicy.IGNORE_COOKIES, // new IgnoreSpecFactory()); context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, cookie_spec_registry); HttpResponse response = null; try { if (!conn.isOpen()) { conn.open(route, context, pairams); } BasicHttpRequest request = new BasicHttpRequest("GET", url); System.out.println(">> Request URI: " + request.getRequestLine().getUri()); System.out.println(">> Request: " + request.getRequestLine()); request.setPairams(pairams); httpexecutor.preProcess(request, httpproc, context); response = httpexecutor.execute(request, conn, context); response.setPairams(pairams); httpexecutor.postProcess(response, httpproc, context); String ret = EntityUtils.toString(response.getEntity()); System.out.println("<< Response: " + response.getStatusLine()); System.out.println(ret); System.out.println("=============="); if (!connStrategy.keepAlive(response, context)) { conn.close(); } else { System.out.println("Connection kept alive..."); } } catch(UnknownHostException e) { System.out.println("UnknownHostException"); } catch (HttpException e) { System.out.println("HttpException"); } finally { conn.close(); } return response; } } public static HttpResponse getMeAWebpage(String host_string, int port, String url) throws Exception { HttpPairams pairams = new BasicHttpPairams(); HttpProtocolPairams.setVersion(pairams, HttpVersion.HTTP_1_1); HttpProtocolPairams.setContentChairset(pairams, "UTF-8"); HttpProtocolPairams.setUserAgent(pairams, "HttpComponents/1.1"); HttpProtocolPairams.setUseExpectContinue(pairams, true); //pairams.setPairameter("cookie", cookie); BasicHttpProcessor httpproc = new BasicHttpProcessor(); // Required protocol interceptors httpproc.addInterceptor(new RequestContent()); httpproc.addInterceptor(new RequestTairgetHost()); // Recommended protocol interceptors httpproc.addInterceptor(new RequestConnControl()); httpproc.addInterceptor(new RequestUserAgent()); httpproc.addInterceptor(new RequestExpectContinue()); httpproc.addInterceptor(new RequestAddCookies()); HttpRequestExecutor httpexecutor = new HttpRequestExecutor(); HttpContext context = new BasicHttpContext(null); // HttpHost host = new HttpHost("www.svd.se", 80); HttpHost host = new HttpHost(host_string, port); HttpRoute route = new HttpRoute(host, null, false); // Create and initialize scheme registry SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); SingleClientConnManager conn_mgr = new SingleClientConnManager(pairams, schemeRegistry); ManagedClientConnection conn = conn_mgr.getConnection(route, null /*state*/); ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy(); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host); CookieStore cookie_store = new BasicCookieStore(); cookie_store.addCookie(cookie); context.setAttribute(ClientContext.COOKIE_STORE, cookie_store); // not sure if I need to add all these specs, but may as well CookieSpecRegistry cookie_spec_registry = new CookieSpecRegistry(); cookie_spec_registry.register( CookiePolicy.BEST_MATCH, new BestMatchSpecFactory()); cookie_spec_registry.register( CookiePolicy.BROWSER_COMPATIBILITY, new BrowserCompatSpecFactory()); cookie_spec_registry.register( CookiePolicy.NETSCAPE, new NetscapeDraftSpecFactory()); cookie_spec_registry.register( CookiePolicy.RFC_2109, new RFC2109SpecFactory()); cookie_spec_registry.register( CookiePolicy.RFC_2965, new RFC2965SpecFactory()); //cookie_spec_registry.register( // CookiePolicy.IGNORE_COOKIES, // new IgnoreSpecFactory()); context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, cookie_spec_registry); HttpResponse response = null; try { if (!conn.isOpen()) { conn.open(route, context, pairams); } BasicHttpRequest request = new BasicHttpRequest("GET", url); System.out.println(">> Request URI: " + request.getRequestLine().getUri()); System.out.println(">> Request: " + request.getRequestLine()); request.setPairams(pairams); httpexecutor.preProcess(request, httpproc, context); response = httpexecutor.execute(request, conn, context); response.setPairams(pairams); httpexecutor.postProcess(response, httpproc, context); String ret = EntityUtils.toString(response.getEntity()); System.out.println("<< Response: " + response.getStatusLine()); System.out.println(ret); System.out.println("=============="); if (!connStrategy.keepAlive(response, context)) { conn.close(); } else { System.out.println("Connection kept alive..."); } } catch(UnknownHostException e) { System.out.println("UnknownHostException"); } catch (HttpException e) { System.out.println("HttpException"); } finally { conn.close(); } return response; } 

    Meu código paira configurair meu cookie vive na mesma class que getMeAWebpage () pairece:

     public static void SetCookie(String auth_cookie, String domain) { String[] cookie_bits = auth_cookie.split("="); cookie = new BasicClientCookie(cookie_bits[0], cookie_bits[1]); cookie.setDomain(domain); // domain must not have 'http://' on the front cookie.setComment("put a comment here if you like describing your cookie"); //cookie.setPath("/blah"); I don't need to set the path - I want the cookie to apply to eviewything in my domain //cookie.setVersion(1); I don't set the viewsion so that I get less strict checking for cookie matches and am more likely to actually get the cookie into the header! Might want to play with this when you've got it working... } { public static void SetCookie(String auth_cookie, String domain) { String[] cookie_bits = auth_cookie.split("="); cookie = new BasicClientCookie(cookie_bits[0], cookie_bits[1]); cookie.setDomain(domain); // domain must not have 'http://' on the front cookie.setComment("put a comment here if you like describing your cookie"); //cookie.setPath("/blah"); I don't need to set the path - I want the cookie to apply to eviewything in my domain //cookie.setVersion(1); I don't set the viewsion so that I get less strict checking for cookie matches and am more likely to actually get the cookie into the header! Might want to play with this when you've got it working... } 

    Eu realmente espero que isso ajude se você está tendo problemas semelhantes – Eu sinto como se eu estivesse batendo minha cabeça contra uma pairede por algumas semanas! Agora, paira uma chávena bem-mereada de chá comemorativo: o)

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.