Android版Drupal的饼干转移Q2饼干、Android、Drupal

2023-09-06 19:02:52 作者:与寂寞干杯

previously我问在的Andr​​oid Drupal的饼干转移了解发送从我的Andr​​oid应用程序回到cookies来我的Drupal的网站,我得到了一个很好的答案。整个想法是使一个持久客户端 - 服务器交互。

Previously I asked a question at Android to Drupal cookie transfer about sending cookies from my Android app back to my Drupal website to which I got a very good answer. The entire idea is to enable a persistent Client-Server interaction.

我调整我的code作为的导演,但我还是不能让事情工作的权利。我的code调整为如下:

I adjusted my code as was directed but I still can not get things working right. My code adjustments are below:

protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub

        HttpResponse response;
        HttpClient httpClient   =   new DefaultHttpClient();
        HttpPost httpPost       =   new HttpPost("http://testsite.com/testpoint/node/");

        BasicHttpContext basicContext   =   new BasicHttpContext();
        org.apache.http.client.CookieStore cookiestore  =   new BasicCookieStore();
        basicContext.setAttribute(ClientContext.COOKIE_STORE, cookiestore);
        basicContext.setAttribute(USERPREFERENCES.getString(COOKIE_NAME, ""), USERPREFERENCES.getString(COOKIE_VALUE, "") );
        //USERPREFERENCES.getString(CO0OKIE_NAME, ""), USERPREFERENCES.getString(COOKIE_VALUE, "")

            // TODO Auto-generated method stub
            try{
                List<NameValuePair> nameValuePairs  =   new ArrayList<NameValuePair>();
                nameValuePairs.add( new BasicNameValuePair("node[title]", "sample node from app") );
                nameValuePairs.add( new BasicNameValuePair("node[type]", "story") );
                nameValuePairs.add( new BasicNameValuePair("node[body]", "sample app node body content") );
                httpPost.setEntity( new UrlEncodedFormEntity(nameValuePairs));

                //Execute HTTP post request
                //HttpResponse 
                response    =   httpClient.execute(httpPost, basicContext);

                Log.i("SEEMS TO WORK", response.toString());
                Log.v("CODE", httpPost.getRequestLine().toString() + " - " + response.toString());

            }catch(Exception e){
                Log.e("HTTP-ERROR: node creation", e.toString());
            }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        //Toast.makeText(getApplicationContext(), "Create Node thread returning!", Toast.LENGTH_LONG).show();
        Toast.makeText(getApplicationContext(), "Testing header: Cookie: " + USERPREFERENCES.getString(COOKIE_NAME, "") +","+ USERPREFERENCES.getString(COOKIE_VALUE, ""), Toast.LENGTH_LONG).show();
    }

}

基本上,我的cookie的名称和值都存储在我的共享$ P $ USER pferences preFERENCES.getString(COOKIE_NAME,)和USER preFERENCES.getString(COOKIE_VALUE,)。我试图将这些信息发送回,这样当该功能执行,并成功创建在我的Drupal网站一个新的故事节点,笔者认为是谁注册的用户,并从Android应用中签署的。

Basically, my cookie name and value are stored in my shared preferences USERPREFERENCES.getString(COOKIE_NAME, "") and USERPREFERENCES.getString(COOKIE_VALUE, ""). I am trying to send that information back so that when this function executes and successfully creates a new story node on my Drupal site, the author is recognized as the user who registered and signed in from the Android app.

节点,但用户是无名氏。因此,我需要发回Drupal的用户cookie信息。

At present, the node is created but the user is "anonymous". Hence, I need to send back Drupal's user cookie information.

任何人都可以请帮助我?

Can anyone please assist me?

推荐答案

这不一定是code解决您的问题,但可能会有所帮助。

This is not necessarily a code solution to your problem, but may be helpful.

当我接近这样的问题,我需要一种方法来查看请求头,并从我的应用程序是什么样子。

When I'm approaching problems like this I need a way to see what the request headers to and from my app look like.

我通常使用查尔斯(伟大的工具) http://www.charlesproxy.com/ 这一点。而且没有我没有对公司有任何联系,它只是一个很好的工具。

I typically use Charles (great tool) http://www.charlesproxy.com/ for this. And no I don't have any connection to the company, it's just a great tool.

有一个功能,在查尔斯称为反向代理,基本上它可以让你通过查尔斯反弹流量到你的Drupal服务器和它的流动,并从您的应用程序可以检查它。

There's a feature in Charles called a reverse proxy and basically it allows you to bounce traffic through Charles to your drupal server and you can inspect it as it flows to and from your app.

使用查尔斯你可以嗅出了什么,从你的浏览器一个很好的请求,看起来像,然后就可以嗅出从Android应用程序的要求是什么样子。比较两个,你可以看到你的应用程序被严重塑造请求头。

Using charles you can sniff what a good request from your web browser looks like and then you can sniff what the requests from your android app look like. Compare the two and you can see where your app is badly shaping the request headers.

调试阶段是这样的:

一旦你得到了查尔斯成立,打你的Drupal服务器几次,检查请求/响应的,你从浏览器中看到的结构。

Once you've got Charles set up, hit your drupal server a couple of times and inspect the structure of the request/response that you're seeing from the browser.

然后打你的服务从Android应用了几次,并注意区别。也许cookie不会经历,也许是畸形的,也许还有其他有关的东西头。这将让你看到你需要拍摄的,以获得Drupal的接受请求的。

Then hit your service a couple of times from the android app and note the differences. Maybe the cookie isn't going through, maybe it's malformed, maybe there's something else about the headers. This will let you see what you need to shoot for in order to get Drupal to accept the requests.

一些说明 -

反向代理将允许你检查流量从您的手机来来往往您的Drupal站点(包括标题,所以你应该可以看到正在发送什么饼干)。要获得反向代理设置,您就需要去反向代理菜单下的代理,并添加新的反向代理 - 代理将采取冲着你的本地主机的流量:端口和重新路由到您的Drupal服务器。它会记录通信在两个方向去。此外,您还可以打你的Drupal服务几次从Firefox,观看Web应用程序如何验证,所以你知道你的目标是什么了。

The reverse proxy will allow you to inspect traffic coming from your phone and going to your drupal site (including headers so you should see exactly what cookies are being sent). To get the reverse proxy set up, you'll need to go to reverse proxies under the proxy menu and add a new reverse proxy - the proxy will take traffic directed at your localhost:PORTNUM and re-route it to your drupal server. It'll log communications going in both directions. Also, you can hit your drupal service a couple of times from FireFox and watch how a web app authenticates so you know what you're aiming for.

为了看手机流量,你需要确保你的手机连接到同一网络的计算机。所以它解决了Web请求到本地计算机上重新写你的电话code。如果你已经拥有了一切正确的配置,然后查尔斯会弹出一个消息,询问您是否它的确定,让您的手机进行连接。之后,你可以看看有什么关于你从什么Web服务器发送发送头不同。

In order to see phone traffic, you'll need to be sure your phone is connected to the same network as your computer. Re-write your phone code so that it addresses its web requests to your local computer. If you've got everything configured correctly then Charles will pop up a messages asking you if it's ok to allow your phone to connect. After that you can see what's different about the headers you're sending from what the web server is sending.