Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用带有web身份验证的apache httpclient进行http post?_Java_Android_Post_Httpclient - Fatal编程技术网

Java 如何使用带有web身份验证的apache httpclient进行http post?

Java 如何使用带有web身份验证的apache httpclient进行http post?,java,android,post,httpclient,Java,Android,Post,Httpclient,我为此搜索了很多,但找不到一个合适的解决方案。使用凭据提供程序的调用是不好的,因为它的调用量是所需调用量的两倍,即它触发请求,获得401,然后才使用web auth凭据触发请求 任何使用android的httpclient库成功向web auth背后的URL发送http post请求的人 对于HttpClient 4.0.x,您使用HttpRequestInterceptor来启用抢占式身份验证-因为AndroidHttpClient类没有公开addRequestInterceptor(..)方

我为此搜索了很多,但找不到一个合适的解决方案。使用凭据提供程序的调用是不好的,因为它的调用量是所需调用量的两倍,即它触发请求,获得401,然后才使用web auth凭据触发请求


任何使用android的httpclient库成功向web auth背后的URL发送http post请求的人

对于HttpClient 4.0.x,您使用HttpRequestInterceptor来启用抢占式身份验证-因为
AndroidHttpClient
类没有公开
addRequestInterceptor(..)
方法,您可能必须使用
DefaultHttpClient

此示例将向感兴趣的任何服务器发送垃圾邮件
user1
/
user1
。如果您关心一点安全性,请调整
AuthScope

DefaultHttpClient client = new DefaultHttpClient(); client.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("user1", "user1")); client.addRequestInterceptor(new HttpRequestInterceptor() { public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { AuthState state = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); if (state.getAuthScheme() == null) { BasicScheme scheme = new BasicScheme(); CredentialsProvider credentialsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER); Credentials credentials = credentialsProvider.getCredentials(AuthScope.ANY); if (credentials == null) { throw new HttpException(); } state.setAuthScope(AuthScope.ANY); state.setAuthScheme(scheme); state.setCredentials(credentials); } } }, 0); // 0 = first, and you really want to be first. DefaultHttpClient=新的DefaultHttpClient(); client.getCredentialsProvider().setCredentials(AuthScope.ANY,新用户名密码凭据(“user1”、“user1”); client.addRequestInterceptor(新的HttpRequestInterceptor(){ 公共无效进程(HttpRequest请求,HttpContext上下文)抛出HttpException,IOException{ AuthState=(AuthState)context.getAttribute(ClientContext.TARGET\u AUTH\u state); if(state.getAuthScheme()==null){ 基本方案=新的基本方案(); CredentialsProvider=(CredentialsProvider)context.getAttribute(ClientContext.CREDS\u PROVIDER); Credentials Credentials=credentialsProvider.getCredentials(AuthScope.ANY); 如果(凭据==null){ 抛出新的HttpException(); } state.setAuthScope(AuthScope.ANY); state.setAuthScheme(scheme); state.setCredentials(凭证); } } }, 0); // 0=第一,你真的想成为第一。
Thanx一吨,我会试试这个。。发现同样的事情,但我一定做错了什么。标志0代表什么,没有真正理解它0是请求拦截器的位置,即0是第一个,它将首先处理请求。@Nitin Jeans给出的这个解决方案&在这个链接上对你有用吗?我也面临着同样的问题。