I need a persistent Http connection over which I will receive multiple XML
responses. Each XML response should be a chunk from the server.
I'm using httpclient-4.2.3 and the following code. Currently, the HttpClient
blocks on the inputStream.read() on the character following the of the chunk.
The read eventually (~15 seconds) returns and things continue. I should note
that the first read() fills the buffer with the 180 (b4) chars of the first
chunk. It's the second read that blocks.
Is there a way to get access to the chunk size prior to the read? In the case
capture in the DEBUG output below, it's 180 (b4) chars.
Or is there a better way to read this full chunk, even if it spans multiple
buffers and therefore multiple reads?
==
try {
response = httpClient.execute(post);
System.out.println("OUTPUT: " + Runner.getURIBase() +
request.getURIPath() + "\n" + postContent);
HttpEntity entity = response.getEntity();
byte[] buffer = new byte[1024];
if (entity != null) {
InputStream inputStream = entity.getContent();
try {
while (!done) {
StringBuilder sb = new
StringBuilder();
int bytesRead = 0;
while ((bytesRead =
inputStream.read(buffer)) != -1) {
String chunk = new
String(buffer, 0, bytesRead);
System.out.println("CHUNK?: " + chunk);
sb.append(chunk);
}
// TODO: Need to check here for
multiple messages in the same chunk
if (sb.length() > 0) {
System.out.println("CHUNK: " + sb.toString());
BroadWorksResponse
broadworksResponse = BroadWorksResponse
.getResponse(sb.toString());
if (broadworksResponse
!= null)
broadworksResponse.fireNextEvent();
sb.setLength(0);
}
//
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (Exception ignore) {
}
}
}
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
DEBUG [org.apache.http.impl.conn.DefaultClientConnection] Sending request: POST
/com.broadsoft.async/com.broadsoft.xsi-events/v2.0/channel HTTP/1.1
DEBUG [org.apache.http.wire] >> "POST
/com.broadsoft.async/com.broadsoft.xsi-events/v2.0/channel HTTP/1.1[\r][\n]"
DEBUG [org.apache.http.wire] >> "Content-Length: 233[\r][\n]"
DEBUG [org.apache.http.wire] >> "Content-Type: text/plain;
charset=ISO-8859-1[\r][\n]"
DEBUG [org.apache.http.wire] >> "Host: ews1.vwave.net:80[\r][\n]"
DEBUG [org.apache.http.wire] >> "Connection: Keep-Alive[\r][\n]"
DEBUG [org.apache.http.wire] >> "User-Agent: Apache-HttpClient/4.2.3 (java
1.5)[\r][\n]"
DEBUG [org.apache.http.wire] >> "Cookie:
JSESSIONID=C041612511AE5606103DC6CDB2C79973[\r][\n]"
DEBUG [org.apache.http.wire] >> "Cookie2: $Version=1[\r][\n]"
DEBUG [org.apache.http.wire] >> "Authorization: Basic
MjI5MzE2MDAxMkB2d2F2ZS5uZXQ6c2lnbmFsbWUwMQ==[\r][\n]"
DEBUG [org.apache.http.wire] >> "[\r][\n]"
DEBUG [org.apache.http.headers] >> POST
/com.broadsoft.async/com.broadsoft.xsi-events/v2.0/channel HTTP/1.1
DEBUG [org.apache.http.headers] >> Content-Length: 233
DEBUG [org.apache.http.headers] >> Content-Type: text/plain; charset=ISO-8859-1
DEBUG [org.apache.http.headers] >> Host: ews1.vwave.net:80
DEBUG [org.apache.http.headers] >> Connection: Keep-Alive
DEBUG [org.apache.http.headers] >> User-Agent: Apache-HttpClient/4.2.3 (java
1.5)
DEBUG [org.apache.http.headers] >> Cookie:
JSESSIONID=C041612511AE5606103DC6CDB2C79973
DEBUG [org.apache.http.headers] >> Cookie2: $Version=1
DEBUG [org.apache.http.headers] >> Authorization: Basic
MjI5MzE2MDAxMkB2d2F2ZS5uZXQ6c2lnbmFsbWUwMQ==
DEBUG [org.apache.http.wire] >> "<?xml version="1.0" encoding="UTF-8"
standalone="no"?>[\n]"
DEBUG [org.apache.http.wire] >> "<Channel
xmlns="http://schema.broadsoft.com/xsi">[\n]"
DEBUG [org.apache.http.wire] >>
"<channelSetId>TempTestChannelSetId</channelSetId>[\n]"
DEBUG [org.apache.http.wire] >> "<priority>1</priority>[\n]"
DEBUG [org.apache.http.wire] >> "<weight>50</weight>[\n]"
DEBUG [org.apache.http.wire] >> "<expires>3600</expires>[\n]"
DEBUG [org.apache.http.wire] >> "</Channel>[\n]"
DEBUG [org.apache.http.wire] << "HTTP/1.1 200 OK[\r][\n]"
DEBUG [org.apache.http.wire] << "Date: Mon, 01 Apr 2013 21:21:46 GMT[\r][\n]"
DEBUG [org.apache.http.wire] << "Server: Apache-Coyote/1.1[\r][\n]"
DEBUG [org.apache.http.wire] << "Expires: Thu, 01 Jan 1970 00:00:00 GMT[\r][\n]"
DEBUG [org.apache.http.wire] << "Content-Language: en-US[\r][\n]"
DEBUG [org.apache.http.wire] << "Cache-Control: no-store[\r][\n]"
DEBUG [org.apache.http.wire] << "Pragma: no-cache[\r][\n]"
DEBUG [org.apache.http.wire] << "Content-Type:
application/xml;charset=UTF-8[\r][\n]"
DEBUG [org.apache.http.wire] << "Set-Cookie:
JSESSIONID=BC1A855C506266CB346506641C735FFC;
Path=/com.broadsoft.xsi-events[\r][\n]"
DEBUG [org.apache.http.wire] << "Keep-Alive: timeout=15, max=98[\r][\n]"
DEBUG [org.apache.http.wire] << "Connection: Keep-Alive[\r][\n]"
DEBUG [org.apache.http.wire] << "Transfer-Encoding: chunked[\r][\n]"
DEBUG [org.apache.http.wire] << "[\r][\n]"
DEBUG [org.apache.http.impl.conn.DefaultClientConnection] Receiving response:
HTTP/1.1 200 OK
DEBUG [org.apache.http.headers] << HTTP/1.1 200 OK
DEBUG [org.apache.http.headers] << Date: Mon, 01 Apr 2013 21:21:46 GMT
DEBUG [org.apache.http.headers] << Server: Apache-Coyote/1.1
DEBUG [org.apache.http.headers] << Expires: Thu, 01 Jan 1970 00:00:00 GMT
DEBUG [org.apache.http.headers] << Content-Language: en-US
DEBUG [org.apache.http.headers] << Cache-Control: no-store
DEBUG [org.apache.http.headers] << Pragma: no-cache
DEBUG [org.apache.http.headers] << Content-Type: application/xml;charset=UTF-8
DEBUG [org.apache.http.headers] << Set-Cookie:
JSESSIONID=BC1A855C506266CB346506641C735FFC; Path=/com.broadsoft.xsi-events
DEBUG [org.apache.http.headers] << Keep-Alive: timeout=15, max=98
DEBUG [org.apache.http.headers] << Connection: Keep-Alive
DEBUG [org.apache.http.headers] << Transfer-Encoding: chunked
WARN [org.apache.http.client.protocol.ResponseProcessCookies] Cookie rejected:
"[version: 0][name: JSESSIONID][value:
BC1A855C506266CB346506641C735FFC][domain: ews1.vwave.net][path:
/com.broadsoft.xsi-events][expiry: null]". Illegal path attribute
"/com.broadsoft.xsi-events". Path of origin:
"/com.broadsoft.async/com.broadsoft.xsi-events/v2.0/channel"
DEBUG [org.apache.http.impl.client.DefaultHttpClient] Connection can be kept
alive for 15000 MILLISECONDS
DEBUG [org.apache.http.impl.client.DefaultHttpClient] Authentication succeeded
DEBUG [org.apache.http.impl.client.TargetAuthenticationStrategy] Caching
'basic' auth scheme for http://ews1.vwave.net:80
OUTPUT:
http://ews1.vwave.net:80/com.broadsoft.async/com.broadsoft.xsi-events/v2.0/channel
org.apache.http.entity.StringEntity@78c36c6f
DEBUG [org.apache.http.wire] << "b4[\r][\n]"
DEBUG [org.apache.http.wire] << "<?xml version="1.0" encoding="UTF-8"?>[\n]"
DEBUG [org.apache.http.wire] << "<Channel
xmlns="http://schema.broadsoft.com/xsi"><channelId>c9d5b432-db96-4256-8771-cdb6e3a1e897</channelId><expires>3600</expires></Channel>"
CHUNK?: <?xml version="1.0" encoding="UTF-8"?>
<Channel
xmlns="http://schema.broadsoft.com/xsi"><channelId>c9d5b432-db96-4256-8771-cdb6e3a1e897</channelId><expires>3600</expires></Channel>
DEBUG [org.apache.http.wire] << "[\r][\n]"
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]