带有settimeout的HttpURLConnection或HttpPost for java 1.4

时间:2014-09-22 06:14:54

标签: settimeout httpurlconnection

如果重复或重复,我很抱歉,需要建议httppostHttpURLConnection setReadTimeoutsetConnectTimeout拨打网址15秒,问题是我正在使用java 1.4,setReadTimeout()setConnectTimeout()不适用于java.net下的java 1.4包,任何其​​他替代方式,下面是我的代码

URL url;                                                   
HttpURLConnection connection = null;                       
url = new URL(apiURL);                                     
connection = (HttpURLConnection)url.openConnection();      
connection.setRequestMethod("POST");                       
connection.setRequestProperty("Content-Type", "text/json");
connection.setRequestProperty("Content-Length", ""+Integer.
connection.setRequestProperty("Content-Language", "en-US");

我的其他代码

HttpClient httpClient = new DefaultHttpClient();        
HttpPost postRequest = new HttpPost(apiURL);      
StringEntity input = new StringEntity(jsonBvmMEssage);     
input.setContentType("text/json");                    
postRequest.setEntity(input);                                                                                  
HttpResponse response = httpClient.execute(postRequest);

提前致谢

1 个答案:

答案 0 :(得分:0)

最后,我发现了这个,

HttpPost postRequest = new HttpPost(apiURL);                    
HttpParams httpParameters = new BasicHttpParams();

int timeoutConnection = 15000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);

int timeoutSocket = 15000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpResponse response = httpClient.execute(postRequest);

希望这会有效,将会测试和更新