2014-07-23 115 views
1

我使用Jetty 7的ContentExchange類來設置URL,標頭和requestContent。

下面是我使用的是哪種碼頭7
碼頭9 - 如何使用Jetty 9設置「請求內容」和「請求標題」?

HttpClient client = new HttpClient(); 
    client.start(); 
    ContentExchange exchange = new ContentExchange(); 
    exchange.setURL(url); 
    exchange.setRequestHeader("Content-Type", "application/xml"); 
    exchange.setRequestHeader("Authorization", "Basic " + this.getAuthenticatonString()); 
    exchange.setMethod("POST"); 

    final AbstractBuffer cb = new ByteArrayBuffer(xml.getBytes("UTF-8")); 
    exchange.setRequestContent(cb); 

    client.send(exchange); 
    exchange.waitForDone(); 


碼頭9沒有ContentExchange類,但是經過一番研究,我有這個

HttpClient client = new HttpClient(); 
client 
.POST(url) 
.send() 


我想我可以使用client.param來設置請求標題,但我不太確定。

所以我的問題是如何使用Jetty 9設置「請求內容」和「請求標題」?

+0

有沒有什麼適合你的? – raevilman

+1

不!我只是繼續使用Jetty 7一段時間,然後切換到澤西島。 – Raj

回答

0
import org.eclipse.jetty.client.HttpClient; 
import org.eclipse.jetty.client.Request; 
import org.eclipse.jetty.client.ContentResponse; 
import org.eclipse.jetty.http.HttpMethod; 
import org.eclipse.jetty.http.HttpHeader; 
... 
HttpClient client = new HttpClient(); 
client.start(); 
Request req = client.newRequest("http://...."); 
req.method(HttpMethod.GET); 
req.agent("Mozilla/5.0..."); 
req.header(HttpHeader.ACCEPT, "text/html"); 
req.header(HtppHeader.CONNECTION, "Keep-Alive"); 
ContentResponse resp = req.send(): 
... 
client.stop();