简体   繁体   English

重用org.apache.commons.httpclient.methods.PostMethod对象时出现问题

[英]Problems when reusing a org.apache.commons.httpclient.methods.PostMethod object

I've noticed that if I reuse a PostMethod object by calling setRequestEntity(...), there are errors on the server side because the request is not parsed correctly. 我注意到,如果我通过调用setRequestEntity(...)重用PostMethod对象,则服务器端会出现错误,因为请求的解析不正确。 It seems as though the length of the body of the request is set the first time I call setRequestEntity, and this length is not being reset after the second time I call setRequestEntity. 似乎请求的主体长度是在我第一次调用setRequestEntity时设置的,而在我第二次调用setRequestEntity之后,此长度并未被重置。

Everything works fine if I use a separate PostMethod object for each request, but is there a way to reuse a PostMethod object for different requests since calling setRequestEntity(...) alone is not sufficient to update the content of the request? 如果我为每个请求使用单独的PostMethod对象,那么一切工作正常,但是由于仅调用setRequestEntity(...)不足以更新请求的内容,有没有办法针对不同的请求重用PostMethod对象?

No. You can reuse HttpClient instances but not Method instances. 不能。您可以重用HttpClient实例,但不能重用Method实例。 They are normally one time use. 它们通常是一次使用。

Don't worry about reusing PostMethod objects, just create new ones. 不必担心重复使用PostMethod对象,只需创建新对象即可。 The JVM nowadays quickly identifies such short lived objects and collects them. 如今,JVM可以快速识别并保留这些短暂的对象。

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

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