【问题标题】:how do I retrieve the session token from httpResponse?如何从 httpResponse 检索会话令牌?
【发布时间】:2011-11-29 04:28:18
【问题描述】:

我有一个描述如下的 api:

给定一个典型的 HTML 表单如下:

 <form method="post" action="/api">
       <input type="hidden" name="action" value="login" />
      <input type="hidden" name="username" value="testuser" />
      <input type="hidden" name="password" value="123" />
 </form>

当上面的 HTML 表单提交时,下面是一个例子说明如何 可以使用 HTTP 协议发送信息:

 POST /api HTTP/1.0
 Content-Type: application/x-www-form-urlencoded
 Content-Length: 43
 action=login&username=testuser&password=123

系统会发回如下回复:

HTTP/1.0 200 OK
Content-Type: application/json
{ 'session_token':'a1234aa334567432bccdd001f123450abcedfa0b' }

谁能指导我如何使用 android java 从发布响应中检索会话令牌?

编辑:(这是我发送和检索请求和响应的一些代码。)

  HttpPost httpost = new HttpPost("url of server");
        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
        nvps.add(new BasicNameValuePair("username","testuser" ));
        nvps.add(new BasicNameValuePair("password", "1234567" ));
        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
        HttpResponse response = getResponse(httpost);

public HttpResponse getResponse(HttpPost httpPost) throws Exception {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpResponse response = httpclient.execute(httpPost);
    return response;
}

【问题讨论】:

标签: java android http-post httpresponse


【解决方案1】:

我是 Android API 的新手,但想提出以下建议:

选项1
Header[] headers = httpResponse.getAllHeaders();

选项2
Header[] headers = httpResponse.getHeaders( "session_token_name_here" );

迭代 headers 数组以找到所需的标头,例如 session_token

【讨论】:

    猜你喜欢
    • 2019-06-22
    • 2016-06-10
    • 2017-01-09
    • 2013-09-06
    • 1970-01-01
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 2016-08-18
    相关资源
    最近更新 更多