英文:
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("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;
}
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("user_token")
. You can do this in servlet for example
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论