如何从URL中获取参数值作为字符串

huangapple go评论63阅读模式
英文:

How do i get parameter value from url as string

问题

我有一个请求,其响应显示在以下URL中。我需要从URL中提取user_token的值,并传递给后续的请求。
响应URL:http://example.com?user_token=0c1c59bc-3aaa-40f1-b978-7172de09a27f&m_id=9999&code=200&is_register=false&M=SUCCESS

我想从中提取user_token,并希望在后续请求中传递它。需要使用Java代码的解决方案,而不是JavaScript。

英文:

I have one request which response is displayed as url below. I need to extract user_token value from url and pass to subsequent request.
response url: http://example.com?user_token=0c1c59bc-3aaa-40f1-b978-7172de09a27f&m_id=9999&code=200&is_register=false&M=SUCCESS

i want to extract user_token from it and want to pass it to subsequent request, need solution in java code not java script.

答案1

得分: 0

你可以这样做:

String getTokenId(String url) {
    String[] splitUrl = url.split("user_token=");
    String tokenId = "";

    if (splitUrl.length > 1) {
        for (int i = 0; i < splitUrl[1].length(); i++) {
            if (splitUrl[1].charAt(i) == '&') {
                tokenId = splitUrl[1].substring(0, i);
                break;
            }
        }
    }

    return tokenId;
}

但如果你知道令牌的确切长度,也可以进行搜索。

英文:

You can do this:

String getTokenId(String url){

String[] splitUrl = url.split(&quot;user_token=&quot;);
String tokenId = &quot;&quot;;

if(splitUrl.length &gt;1){

for(int i =0; i &lt; splitUrl[1].length(); i++){
    if(splitUrl[1].charAt(i) == &#39;&amp;&#39;){
        tokenId = splitUrl[1].substring(0,i);
        break;
    }
}

}

return  tokenId;
}

But if you know the exact length of the token, it could be a search.

答案2

得分: 0

也许你需要的是 request.getParameter("user_token")。你可以在Servlet中这样做,例如:

英文:

Maybe what you need is request.getParameter(&quot;user_token&quot;). You can do this in servlet for example

huangapple
  • 本文由 发表于 2020年8月8日 04:01:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63308487.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定