简体   繁体   English

带参数的 URL GET 请求

[英]URL GET request with parameters

For example I have this string:例如我有这个字符串:

String url = "http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?username=USERNAME&password=PASSWORD";

and I have the String USERNAME="123456" and String PASSWORD="123";我有String USERNAME="123456"String PASSWORD="123"; When I make the URL GET request how do I inject the USERNAME and PASSWORD into the URL?当我发出 URL GET 请求时,如何将 USERNAME 和 PASSWORD 注入 URL?

You can just use the Replace function.您可以只使用替换功能。

This will give you the following code:这将为您提供以下代码:

String url = "http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?username=USERNAME&password=PASSWORD";
String USERNAME="123456";
String PASSWORD="123";

url = url.replace("?username=USERNAME&password=PASSWORD","username="+USERNAME+"&password="+PASSWORD);

Although it doesn't seem wise to send a Username and Password in the querystring!尽管在查询字符串中发送用户名和密码似乎并不明智! If no other option is available be sure to use https so this is not visible for sniffing tools.如果没有其他选项可用,请务必使用 https,以便嗅探工具不可见。 Otherwise use the default authentication.否则使用默认身份验证。

You can create a method like below您可以创建一个如下所示的方法

public String getUrl(String Username,String Password){
    String url = "http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?username="+Username+"&password="+Password;
    return url;
}

After that you can call this method之后你可以调用这个方法

String URL = getUrl("Username","Password");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM