登录后的JAVA HttpClient 401
我精通java和web编程。但我必须为android编写应用程序,它可以与http一起工作。 我为http请求编写了类,但有一个问题:当我发送另一个请求时,授权后出现错误401。 它可以是什么?我怎样才能修理它? 谢谢你的帮助登录后的JAVA HttpClient 401,java,android,http,web,httpclient,Java,Android,Http,Web,Httpclient,我精通java和web编程。但我必须为android编写应用程序,它可以与http一起工作。 我为http请求编写了类,但有一个问题:当我发送另一个请求时,授权后出现错误401。 它可以是什么?我怎样才能修理它? 谢谢你的帮助 public void SendRequest(final String url) { Thread t = new Thread(new Runnable() { @Override public voi
public void SendRequest(final String url) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
URI website = null;
try {
website = new URI(baseUri+url);
} catch (URISyntaxException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = null;
try {
response = client.execute(request, localContext);
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
if (response != null)
{
HttpEntity entity = response.getEntity();
try {
InputStream is = entity.getContent();
httpEvent.HttpResponseArrived(convertStreamToString(is),response.getStatusLine().getStatusCode());
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
});
t.start();
}
public void Login(String login, String password, boolean isRemember)
{
String mess = "logon?username="+login+"&password="+password+"&remember="+ Boolean.toString(isRemember);
SendRequest(mess);
httpEvent = new HttpHandler()
{
@Override
public void HttpResponseArrived(String data, int code)
{
String mess = "Error";
switch (code)
{
case 401:
mess = "Incorrect login/password";
break;
case 200:
mess = "";
break;
}
events.LoginArrived(Boolean.valueOf(data), mess);
}
};
}
您是否发送了会话id以便服务器识别您?I jast发送了登录名和密码:)当您登录到某个对象时,会在服务器上保存一个会话以记住您。cookie可能会被发回,您应该在以后的请求中使用它。我尝试过类似的方法,但它不起作用:CookieStore CookieStore=new BasicCookieStore();HttpContext HttpContext=新的BasicHttpContext();setAttribute(ClientContext.COOKIE_存储,cookieStore);你有没有问过自己这些代码行是做什么的?这些有什么帮助吗?