繁体   English   中英

此Dispatch Scala的等效HttpClient Java代码是什么?

[英]What's the equivalent HttpClient Java code for this Dispatch Scala?

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的误解。 我知道了,基本上我在传递带有协议的整个URL,即“ https://mysite.com”,而不只是“ mysite.com”,我解决了这个问题。

这是对AuthScope的误解。 我知道了,基本上我在传递带有协议的整个URL,即“ https://mysite.com”,而不只是“ mysite.com”,我解决了这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM