Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xutils3使用Digest验证的方法 #191

Closed
rachelhq opened this issue Feb 17, 2016 · 9 comments
Closed

xutils3使用Digest验证的方法 #191

rachelhq opened this issue Feb 17, 2016 · 9 comments

Comments

@rachelhq
Copy link

需要使用digest验证,xutil2使用起来比较方便,可以使用httpclient;
但是xutils3好像用起来比较麻烦,现在使用的方式如下:

1、首先需要使用异步调用方式:
x.http().post(requestParams, new CustomCallback());

2、CustomCallback 需要继承 InterceptRequestListener,才能在afterRequest(UriRequest request)回调中拿到response中header "WWW-Authenticate"的值

3、需要digest验证的请求,第一次post之后会回调onError(Throwable ex, boolean isOnCallback);
在该回调方法中判断httpcode是否为HTTP_UNAUTHORIZED,是的话自己拼接DigestAuth字符串authString,并且在requestparams中加入新的header:
requestParams.addHeader("Authorization", authString);

4、再一次调用x.http().post(requestParams, new CustomCallback());

这么使用起来有几个限制:必须使用异步方式调用,必须继承InterceptRequestListener,必须在onerror中自己拼接验证heder。。。

请问有没有比较方便的方式?多谢

@wyouflf
Copy link
Owner

wyouflf commented Feb 17, 2016

使用自定义ParamsBuilder, 看示例代码:
https://github.com/wyouflf/xUtils3/blob/master/sample/src/main/java/org/xutils/sample/http/JsonDemoParams.java

https://github.com/wyouflf/xUtils3/blob/master/sample/src/main/java/org/xutils/sample/http/JsonDemoParamsBuilder.java

如果需要在HTTP_UNAUTHORIZED的时候重试, 那么在ParamsBuilder#buildParams方法中加入params.setHttpRetryHandler(.....), 自定义重试机制: 加入的验证字段, canRetry返回true.

这样就不必在callback中关心参数和错误重试的问题, 也不用再次发送请求, 重试是自动的.

如果需要自动解析定义的请求结果参考示例中的:
https://github.com/wyouflf/xUtils3/blob/master/sample/src/main/java/org/xutils/sample/http/JsonResponseParser.java (如果使用fastjson, 注意注释代码中fastjson的代码)

  /**
   * 1. callback的泛型:
   * callback参数默认支持的泛型类型参见{@link org.xutils.http.loader.LoaderFactory},
   * 例如: 指定泛型为File则可实现文件下载, 使用params.setSaveFilePath(path)指定文件保存的全路径, 默认支持断点续传(采用了文件锁防止多线程/进程修改文件,及文件末端校验续传文件的一致性).
   * 
   * 自定义callback的泛型支持方案1, 自定义某一Class的转换(不够灵活): 
   * 结合PrepareCallback的两个泛型参数, 第一个泛型参数类型使用LoaderFactory已经支持的, 第二个泛型参数作为最终输出, 需要在prepare方法中自己实现.
   * 一个稍复杂的例子可以参考{@link org.xutils.image.ImageLoader}
   *
   * 自定义callback的泛型支持方案2, 自定义一类数据的自动转化: 
   * 将注解@HttpResponse加到自定义返回值类型上, 实现自定义ResponseParser接口来统一转换.
   * 如果返回值是json/xml/protobuf等数据格式, 那么利用第三方的json/xml/protobuf等工具将十分容易定义自己的ResponseParser/InputStreamResponseParser.
   * 如示例代码{@link org.xutils.sample.http.JsonDemoResponse}, 可直接使用JsonDemoResponse作为callback的泛型.
   *
   * 2. callback的组合:
   * 可以用基类或接口组合个种类的Callback, 见{@link org.xutils.common.Callback}.
   * 例如:
   * a. 组合使用CacheCallback将使请求检测缓存或将结果存入缓存(仅GET请求生效).
   * b. 组合使用PrepareCallback的prepare方法将为callback提供一次后台执行耗时任务的机会, 然后将结果给onCache或onSuccess.
   * c. 组合使用ProgressCallback将提供进度回调.
   * 可参考{@link org.xutils.image.ImageLoader} 或 示例代码中的 {@link org.xutils.sample.download.DownloadCallback}
   *
   * 3. 请求过程拦截或记录日志: 参考 {@link org.xutils.http.app.RequestTracker}
   *
   * 4. 请求Header获取: 参考 {@link org.xutils.http.app.RequestInterceptListener}
   *
   * 5. 其他(线程池, 超时, 重定向, 重试, 代理等): 参考 {@link org.xutils.http.RequestParams}
   *
   **/

@rachelhq
Copy link
Author

好的,多谢回答。有几个问题:
1.httpurlconnection有比较方便的方式实现digest auth么?

2.如果没有
按照以上方式,digest验证的信息还需要自己去组装吧?
得在第一次请求失败,发送第二次请求之前组装,并在request的header中加header:addHeader("Authorization", authString);
如果直接通过retry来重试, 这种request的修改可以在自动重试的时候使用上吗?

3.JsonResponseParser只有在请求成功的情况下才会调用,失败的情况下是不会调用的,所以如果需要拿到reponse的header,callback还是必须implements InterceptRequestListener?

多谢

####################
RE:
2. 自定义HttpRetryHandler, 在canRetry方法里直接判断加入验证头信息, 返回true, 就会自动发起第二次请求.
3. 有了上面的HttpRetryHandler, 就不需要在JsonResponseParser做这件事情了, 另外, ResponseParser#checkResponse方法可以拿到头信息, 不需要InterceptRequestListener. 使用ParamsBuilder和ResponseParser就可以将请求模板化, 可以定制各种复杂的请求参数和过程转换模板.

@lanaiver
Copy link

x.http().get(....)重复提交多次请求,如果关闭之前请求,只继续最后一个请求

@combing520
Copy link

HTTP 请求怎样设置请求超时时间? 你这边的默认超时时间好像是15秒。

@xmrkwzw
Copy link

xmrkwzw commented Aug 8, 2016

同样用addbodyParamter传file 为什么2的可以3 不行呢??服务器该怎么接收呢?

@jicg
Copy link

jicg commented Aug 21, 2016

post的请求有点问题吧,发送参数到服务器,后台接收不到啊。

@hxkdidi
Copy link

hxkdidi commented Jan 24, 2017

经常出现socketTimeout,我设置超时时间是7s,但是出现这个错误基本在发出请求的1,2s内,比较困惑!

@wyouflf
Copy link
Owner

wyouflf commented Mar 18, 2019

同样用addbodyParamter传file 为什么2的可以3 不行呢??服务器该怎么接收呢?

setMultipart(true)试试, 默认只有一个body参数,并且是文件类型是使用普通的文件表单上传。

@JavaSnail
Copy link

x.http().get(params, new Callback.CacheCallback() {}方法连接不到后端服务器有可能是哪里出错了,请问一下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants