Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过HttpGet对象检索数据时设置超时值_Java_Http - Fatal编程技术网

Java 通过HttpGet对象检索数据时设置超时值

Java 通过HttpGet对象检索数据时设置超时值,java,http,Java,Http,我从以前的同事那里“继承”了一些Java代码。它的一部分使用方法GET连接到外部URL,并检索少量XML进行解析。我们最近遇到了这样的问题:由于供应商网站挂起并使用了我们这边的资源,连接崩溃了我们的网站。一个问题是,当我们的代码使用HttpGet对象时,没有设置超时。有没有一种方法可以使用此对象微调超时,或者有没有更好的方法可以收回此XML 使用另一个API会更好吗 List<NameValuePair> params = new ArrayList<NameValuePair

我从以前的同事那里“继承”了一些Java代码。它的一部分使用方法GET连接到外部URL,并检索少量XML进行解析。我们最近遇到了这样的问题:由于供应商网站挂起并使用了我们这边的资源,连接崩溃了我们的网站。一个问题是,当我们的代码使用HttpGet对象时,没有设置超时。有没有一种方法可以使用此对象微调超时,或者有没有更好的方法可以收回此XML

使用另一个API会更好吗

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("param1","foobar"));
URI uri = URIUtils.createURI("http", "myhost.com", -1, "mypath",
URLEncodedUtils.format(params, "UTF-8"), null);

// there is no timeout here??
HttpGet httpGet = new HttpGet(uri);
HttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(httpGet);
String result = IOUtils.toString(httpResponse.getEntity()
    .getContent(), "UTF-8");
List params=new ArrayList();
参数添加(新的BasicNameValuePair(“参数1”、“foobar”);
URI=URIUtils.createURI(“http”、“myhost.com”、-1、“mypath”,
URLEncodedUtils.format(参数“UTF-8”),null;
//这里没有超时??
HttpGet HttpGet=新的HttpGet(uri);
HttpClient HttpClient=新的DefaultHttpClient();
HttpResponse HttpResponse=httpClient.execute(httpGet);
String result=IOUtils.toString(httpResponse.getEntity()
.getContent(),“UTF-8”);
谢谢

试试这个

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("param1","foobar"));
URI uri = URIUtils.createURI("http", "myhost.com", -1, "mypath",
URLEncodedUtils.format(params, "UTF-8"), null);

HttpGet httpGet = new HttpGet(uri);
HttpClient httpClient = new DefaultHttpClient();
// set the connection timeout value to 30 seconds (30000 milliseconds)
final HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 30000);
httpClient = new DefaultHttpClient(httpParams);
HttpResponse httpResponse = httpClient.execute(httpGet);
String result = IOUtils.toString(httpResponse.getEntity().getContent(), "UTF-8");
List params=new ArrayList();
参数添加(新的BasicNameValuePair(“参数1”、“foobar”);
URI=URIUtils.createURI(“http”、“myhost.com”、-1、“mypath”,
URLEncodedUtils.format(参数“UTF-8”),null;
HttpGet HttpGet=新的HttpGet(uri);
HttpClient HttpClient=新的DefaultHttpClient();
//将连接超时值设置为30秒(30000毫秒)
最终HttpParams HttpParams=新的基本HttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,30000);
httpClient=新的默认httpClient(httpParams);
HttpResponse HttpResponse=httpClient.execute(httpGet);
字符串结果=IOUtils.toString(httpResponse.getEntity().getContent(),“UTF-8”);

From

可能重复:由于DefaultHttpClient不推荐使用:HttpGet HttpGet=new HttpGet(url.toURI());HttpClient HttpClient=HttpClientBuilder.create().setConnectionTimeOlive(30,TimeUnit.SECONDS).build();HttpResponse HttpResponse=httpClient.execute(httpGet);