2012-01-12 77 views
0
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) 

我已經試過這樣:什麼是這個Dispatch Scala的等效HttpClient Java代碼?

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 w /協議,即「https://mysite.com」而不是「mysite.com」我解決了這個問題。

+0

那麼,以什麼方式不起作用? – 2012-01-13 13:43:29

回答

0

這是對AuthScope的誤解。我想通了,基本上我是通過整個URL w /協議,即「https://mysite.com」而不是「mysite.com」我解決了這個問題。

相關問題