मुझे http प्राप्त करने के हिस्से के रूप में कुकीज़ भेजने में समस्या हो रही है। सबसे पहले मैं एक वेबव्यू के भीतर एक लॉगिन पेज पर जाता हूं जो मुझे एक कुकी देता है। मैंने चेक किया है और कुकी को कुकी मैनेजर में संग्रहीत किया जा रहा है। फिर मैं एक ही डोमेन से एक विशेष यूआरएल प्राप्त करने के लिए BasicHttpRequest का उपयोग करता हूं। मुझे उम्मीद है कि मुझे प्राप्त होने वाली कुकी को मेरे हेडर से मिलने के लिए जोड़ा जाएगा, लेकिन इसे वायरसहार्क में देखकर यह वहां नहीं है। मैंने गुमराह किया है और बहुत सारे प्रश्न पढ़े हैं और यह सुनिश्चित कर लिया है कि:Android पर BasicHttpRequest के साथ वेबव्यू से कुकीज़ साझा करना
- मैं कुकी सिंक मैनेजर का उपयोग कर रहा हूं इसलिए मुझे उम्मीद है कि सत्र से मेरी कुकीज़ जारी रहेगी। मुझे नहीं लगता कि यह कुकी सिंकमैनगर के रूप में असीमित होने में एक समस्या है क्योंकि मैं हर 5 सेकंड में यूआरएल मारता रहता हूं और कुकी कभी नहीं जोड़ा जाता है।
- मुझे संदेह है कि मुझे अपने कुकी स्टोर के बारे में अपना http अनुरोध बताने की ज़रूरत है, लेकिन जिन समाधानों को मैंने गुम किया है, वे मेरे लिए संकलित नहीं हैं। ऐसा लगता है कि मैं कुछ ऐसा करना चाहता हूं जो context.setAttribute (ClientContext.COOKIE_STORE, this.cookieStore) जैसा दिख रहा है, लेकिन मैं यह नहीं समझ सकता कि कुकी मैनेजर से डिफ़ॉल्ट कुकीस्टोर कैसे प्राप्त करें। कुछ कोड cookieManager.getCookieStore() को कॉल करते हैं, लेकिन यह एंड्रॉइड पर मेरे लिए संकलित नहीं करता है। दस्तावेज़ों को देखते हुए मैं कुकीस्टोर को पाने के लिए एक रास्ता नहीं देख सकता जो पागल लगता है - क्या मुझे कुछ याद आ रही है?
मेरी वेबव्यू में प्रवेश पृष्ठ शुरू करने मेरे कोड लगता है:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// use cookies to remember a logged in status
CookieSyncManager.createInstance(this);
CookieSyncManager.getInstance().startSync();
//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_server+"/api/v1/login");
setContentView(webview);
तब कुकी की जाँच करने के लिए अपने कोड है वहाँ की तरह दिखता है:
public static boolean CheckAuthorised() {
CookieSyncManager.getInstance().sync();
CookieManager cookie_manager = CookieManager.getInstance();
String cookie_string = cookie_manager.getCookie("http://"+my_server+"/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_server, cookie_string);
// maybe we need to store the cookie for the url we're actually going to access?
cookie_manager.setCookie("http://"+my_server+"/api/v1/activity", cookie_string);
CookieSyncManager.getInstance().sync();
return true;
}
}
}
return false;
}
और वास्तव में बनाने के लिए http अनुरोध मैं
public static HttpResponse getMeAWebpage(String host_string, int port, String url)
throws Exception {
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
HttpProtocolParams.setUseExpectContinue(params, true);
BasicHttpProcessor httpproc = new BasicHttpProcessor();
// Required protocol interceptors
httpproc.addInterceptor(new RequestContent());
httpproc.addInterceptor(new RequestTargetHost());
// 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, params);
}
BasicHttpRequest request = new BasicHttpRequest("GET", url);
System.out.println(">> Request URI: "
+ request.getRequestLine().getUri());
System.out.println(">> Request: "
+ request.getRequestLine());
request.setParams(params);
httpexecutor.preProcess(request, httpproc, context);
response = httpexecutor.execute(request, conn, context);
response.setParams(params);
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;
}
रीडिन के लिए धन्यवाद जी अब तक! कोई सुझाव कृतज्ञता प्राप्त किया,
एमी
क्या आपने पैराम्स.सेट पैरामीटर ("कुकी", कुकी) की कोशिश की है? जबकि कुकी आपके कुकीस्टोर से वैध कुकी है। –
हाय फ्रांजिस्कस, मैंने अभी पैराम्स.सेट पैरामीटर ("कुकी", कुकी) की कोशिश की है, और मैं देख सकता हूं कि पैराम्स में अब 5 प्रविष्टियां हैं, लेकिन कुकी भाग इसे कभी भी http में वायरसहार्क का उपयोग करके नहीं देखता है। –