使用 HttpClient 4.1 获取查询字符串
我正在从 HttpClient 3.0 迁移到 Httpclient 4.1 如何从 URL 中获取查询字符串。
client = new DefaultHttpClient();
client.getHostConfiguration().setHost( pro.getProperty( "host" ),
Integer.parseInt( pro.getProperty( "port" ).trim() ),
pro.getProperty( "protocol" ) );
//client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
client.getCredentialsProvider().setCredentials(
new AuthScope( pro.getProperty( "host" ),
Integer.parseInt( pro.getProperty( "port" ).trim() ),
pro.getProperty( "protocol" ) ),
new UsernamePasswordCredentials( user, userpassword ) );
client.getParams().setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true );
authget = new HttpGet( pro.getProperty( "getparam" ) );
HttpResponse response = client.execute( authget );
HttpEntity entity = response.getEntity();
我应该在此处插入什么以使查询字符串与“&”相匹配。
StringTokenizer qryStrToken = new StringTokenizer(***Insert code for getting query string***,"&");
while (qryStrToken.hasMoreTokens()){
String temp = qryStrToken.nextToken();
if(temp.startsWith("SMAGENTNAME")){
smAgentName = temp.substring(temp.indexOf("=")+1);
}
}
I am migrating to Httpclient 4.1 from HttpClient 3.0
How can I get the query String from the URL.
client = new DefaultHttpClient();
client.getHostConfiguration().setHost( pro.getProperty( "host" ),
Integer.parseInt( pro.getProperty( "port" ).trim() ),
pro.getProperty( "protocol" ) );
//client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
client.getCredentialsProvider().setCredentials(
new AuthScope( pro.getProperty( "host" ),
Integer.parseInt( pro.getProperty( "port" ).trim() ),
pro.getProperty( "protocol" ) ),
new UsernamePasswordCredentials( user, userpassword ) );
client.getParams().setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true );
authget = new HttpGet( pro.getProperty( "getparam" ) );
HttpResponse response = client.execute( authget );
HttpEntity entity = response.getEntity();
What should I insert here for getting the query string to match from "&".
StringTokenizer qryStrToken = new StringTokenizer(***Insert code for getting query string***,"&");
while (qryStrToken.hasMoreTokens()){
String temp = qryStrToken.nextToken();
if(temp.startsWith("SMAGENTNAME")){
smAgentName = temp.substring(temp.indexOf("=")+1);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用HttpGet的getUri方法。
Use the getUri method of HttpGet.
URI 包含您的查询字符串,您可以通过以下方式获取:
URLEncodedUtils.parse(new URI(request.getRequestLine().getUri()),HTTP.UTF_8);
The URI has your query string which you can get by:
URLEncodedUtils.parse(new URI(request.getRequestLine().getUri()),HTTP.UTF_8);