我在名为HttpHelper的类中使用了这些方法。现在,初始调用验证用户凭据并返回true。这是预料之中的。下一个电话是注册用户。这似乎很好,没有打嗝,并返回新的用户ID。而不是返回用户ID,它再次返回true。几乎就像它被缓存并只返回初始调用的结果。有人想知道为什么会这样吗?
private static CookieStore sCookieStore;
private static String invoke(HttpUriRequest request)
throws ClientProtocolException, IOException {
String result = null;
DefaultHttpClient httpClient = new DefaultHttpClient();
// restore cookie
if (sCookieStore != null) {
httpClient.setCookieStore((org.apache.http.client.CookieStore) sCookieStore);
}
//request.addHeader("Host", "localhost");
HttpResponse response = httpClient.execute(request);
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
for (String s = reader.readLine(); s != null; s = reader.readLine()) {
builder.append(s);
}
result = builder.toString();
Log.d(TAG, "result is ( " + result + " )");
// store cookie
sCookieStore = (CookieStore) ((AbstractHttpClient) httpClient).getCookieStore();
return result;
}
public static String invokeGet(String action, List<NameValuePair> params) {
try {
StringBuilder sb = new StringBuilder(API_URL);
sb.append(action);
if (params != null) {
for (NameValuePair param : params) {
sb.append("?");
sb.append(param.getName());
sb.append("=");
sb.append(param.getValue());
}
}
Log.d(TAG, "url is" + sb.toString());
//HttpGet httpGet = new HttpGet(URLEncoder.encode(sb.toString(), "UTF-8"));
HttpGet httpGet = new HttpGet(sb.toString());
return invoke(httpGet);
} catch (Exception e) {
Log.e(TAG, e.toString());
}
return null;
}
public static String invokeGet(String action) {
return invokeGet(action, null);
}
以下是我正在进行的通话。如上所述,authResult变量将导致字符串结果为true。 nextResult应该保存用户ID值,而是保持值true,就像初始请求一样:
String authResult = HttpHelper.invokeGet("<url to validate user>");
if (this.URL.indexOf("register") > -1)
String nextResult = HttpHelper.invokeGet(this.URL);
else
String nextResult = HttpHelper.invokeGet(this.URL);
更新:添加到sCookieStore
变量中。
更新2 :添加通过API(MVC .NET)调用的方法:
authResult
变量会得到以下结果:
public bool Get(String userName, String userPassword)
{
Task<IdentityUser> iu = _repo.FindUser(userName, userPassword);
if (iu != null)
{
FormsAuthentication.SetAuthCookie(userName, false);
return true;
}
return false;
}
nextResult
变量应该得到这个结果:
[HttpGet]
[AllowAnonymous]
public async Task<IHttpActionResult> RegisterUser(String userName, String userPassword, String huh)
{
UserModel userModel = new UserModel();
userModel.ConfirmPassword = userPassword;
userModel.UserName = userName;
userModel.Password = userPassword;
String userID = "";
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
IdentityResult result = await _repo.RegisterUser(userModel);
IHttpActionResult errorResult = GetErrorResult(result);
if (errorResult != null)
{
return errorResult;
}
else
{
Task<IdentityUser> iu = _repo.FindUser(userModel.UserName, userModel.Password);
using (var context = new AuthContext())
{
var userStore = new UserStore<IdentityUser>(context);
var userManager = new UserManager<IdentityUser>(userStore);
userID = iu.Result.Id;
result = await userManager.AddToRoleAsync(userID, "Users");
errorResult = GetErrorResult(result);
if (errorResult != null)
{
return errorResult;
}
}
}
return Ok("userID:" + userID);
}
答案 0 :(得分:0)
看起来 authResult 将激活 invokeGet 2次,为其提供相同的参数( this.URL )。在 invokeGet 中,变量 sb 将成为函数触发时的 this.URL 。
这对我有点好奇:
// restore cookie
if (sCookieStore != null) {
httpClient.setCookieStore((org.apache.http.client.CookieStore) sCookieStore);
}
然后在最后,
// store cookie
sCookieStore = (CookieStore) ((AbstractHttpClient) httpClient).getCookieStore();
什么时候定义sCookieStore?可能是因为没有正确处理cookie并且您正在建立两个新连接。如果获取和设置cookie是个问题,那么在 invoke 的开头存在一个cookie可能会更好,然后在最后设置一个cookie。