此 Dispatch Scala 的等效 HttpClient Java 代码是什么?
import dispatch._
val h = new Http
val req = url(url).as_!(username, password)
val handler = req << (payload, "application/xml") >:> identity
handler.apply {
case (200, _, Some(entity), _) => (200, Some(entity))
case (status, _,_,_) => (status, None)
}
val response = h(handler)
我已经尝试过:
public static void postService(Profile user, String subject, String body){
Credentials credentials = new UsernamePasswordCredentials(userName, authToken);
AuthScope authScope = new AuthScope(baseUrl, 80, AuthScope.ANY_REALM);
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(baseUrl);
client.getParams().setAuthenticationPreemptive(true);
client.getState().setCredentials(authScope, credentials);
String payload = makePayload(user, subject, body);
RequestEntity entity = null;
try{
entity = new StringRequestEntity(payload, CONTENT_TYPE, null);
} catch(UnsupportedEncodingException e){
e.printStackTrace();
}
post.setRequestEntity(entity);
try{
client.executeMethod(post);
} catch(IOException e){
e.printStackTrace();
} catch(HTTPException e){
e.printStackTrace();
}
}
更新 - 已解决:这是对 AuthScope 的误解。我想通了,基本上我是通过协议传递整个网址,即“https://mysite.com”,而不仅仅是“mysite.com”,我解决了这个问题。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是对 AuthScope 的误解。我想通了,基本上我是通过协议传递整个网址,即“https://mysite.com”,而不仅仅是“mysite.com”,我解决了这个问题。
This was a misunderstanding of AuthScope. I figured it out, basically I was passing in the entire url w/protocol i.e. "https://mysite.com" rather than just "mysite.com" I resolved this issue.