gpt4 book ai didi

java - 具有基本安全性的 REST Web 服务的 Android 客户端

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:06:46 26 4
gpt4 key购买 nike

我使用基本身份验证编写了一个简单的 RESTful Web 服务。我使用了此页面 secureRestWS 中的提示.他们还制作了一段视频并在 youtube 上发布了更多详细信息。当您在浏览器中打开它时,它工作得很好。您需要在授权窗口中填写用户名和密码。

我还找到了一个带有 android 客户端的页面,它使用 httpclient 调用 rest web 服务,如果 web 服务没有身份验证,它就可以工作。我正在使用这个例子 androidRestWSClient .但我不知道如何在这种情况下添加用户名和密码,我尝试过:

client.AddParam("User Name", "myusername");
client.AddParam("Password", "mypassword");

或在标题中:

client.AddHeader("User Name", "myusername");
client.AddHeader("Password", "mypassword");

但没有任何作用。我还尝试创建告诉 Web 服务用户名和密码的 url,例如:

http://192.168.1.42/RestWS/resources/helloWorld?username=myusername&password=mypassword

我真的没有解决方案,所以如果有人有示例客户端,我将不胜感激。

在网络服务中我有简单的 GET 方法

@GET
@Path("/text")
public String getText() {
return "Hello World!";
}

我需要为用户名和密码使用 SecurityContext 吗?像本例中那样手动创建身份验证是否更好:

http://aruld.info/accessing-restful-services-configured-with-ssl-using-resttemplate/

谢谢你的帮助

最佳答案

如果您只使用基本的 http 身份验证,则 URL 应如下所示:

http://username:password@192.168.1.42/RestWS/resources/helloWorld

好吧,给你:

HttpGet httpget;
try{
httpget = new HttpGet(url);

String auth =new String(Base64.encode(( username + ":" + password).getBytes(),Base64.URL_SAFE|Base64.NO_WRAP));
httpget.addHeader("Authorization", "Basic " + auth);
}

关于java - 具有基本安全性的 REST Web 服务的 Android 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8401938/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com