I need to set a ByteArrayRequestEntity
as the entity on an HttpPost
object via the setEntity(...) method which expects an HttpEntity
as a parameter. Via the Netbeans tip, I'm casting the ByteArrayRequestEntity
as an HttpEntity
, but I'm not sure exactly why that is allowable. HttpEntity
is an interface, and I am not sure that the ByteArrayRequestEntity
implements all the methods of that interface (or if it needs to). Can anyone please clarify that this is an allowable casting?
Asked
Active
Viewed 674 times
0

macbombe
- 3
- 2
-
Which version of HttpClient are you using? Are you sure that NetBeans didn't cast to [ByteArrayEntity](http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/entity/ByteArrayEntity.html) instead of [ByteArrayRequestEntity](http://www.javadocexamples.com/org/apache/commons/httpclient/methods/org.apache.commons.httpclient.methods.ByteArrayRequestEntity.html)? – Narmer Aug 19 '14 at 15:37
-
@Narmer I was using a ByteArrayRequestEntity object from commons-httpclient v3.1. I see that the ByteArrayEntity is from the Apache HttpCore library. I switched the object to a ByteArrayEntity which gets rid of the need for the (HttpEntity) casting. Don't fully understand the difference between the two object types (API description and methods for both are similar). Question still stands why Netbeans would tell me to cast (HttpClient) ByteArrayRequestEntity, but OBE, at this point. Thanks for pointing me in the right direction. – macbombe Aug 19 '14 at 15:58
2 Answers
0
You can't cast a ByteArrayRequestEntity to an HttpEntity, but you should be able to create one from it:
new ByteArrayRequestEntity(yourEntity.getContent())
The real problem is that you're mixing libraries. ByteArrayRequestEntity is from the outdated HttpClient 3.x library, and HttpEntity is from the new HttpComponents library, so they aren't directly compatible. If possible, you should switch everything over to HttpComponents.

Sean Van Gorder
- 3,393
- 26
- 26
-
Thanks so much. I realized I was mixing libraries when I looked into both classes. Didn't look like a valid cast, but Netbeans suggested it. Thank you for the tip. – macbombe Aug 19 '14 at 16:10
-2
It doesn't hurt to do some investigation, right?
According to Apache HTTP Components documentation, ByteArrayRequestEntity
implements the interface RequestEntity
, and not HttpEntity

Matias Cicero
- 25,439
- 13
- 82
- 154
-
Right, I did see that ByteArrayRequestEntity implements RequestEntity.. but don't see how RequestEntity and HttpEntity relate (besides 3 overlapping methods). There are still some methods in HttpEntity that are not implemented by ByteArrayRequestEntity, so I'm wondering why Netbeans is allowing me to cast it as an HttpEntity. – macbombe Aug 19 '14 at 14:58
-
1Rude and not responding the OP question, you have just pointed out exactly what the OP is asking. – Narmer Aug 19 '14 at 15:30