Já pro toto používám InputStreamReader s nastaveným kodováním.
private String getDataFrom(URL url, String request) throws IOException {
OutputStream os = null;
InputStream is = null;
InputStreamReader isr = null;
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(HTTP_REQUEST_METHOD_POST);
connection.setRequestProperty(HTTP_CONTENT_TYPE,
CONTENT_TYPE_APPLICATION_XML);
connection.setDoInput(true);
connection.setDoOutput(true);
os = connection.getOutputStream();
os.write(request.getBytes());
os.flush();
is = connection.getInputStream();
isr = new InputStreamReader(is, responseEncoding);
byte [] response = DataUtils.readFrom(isr);
return new String(response);
} finally {
if(os != null) {
try { os.close(); } catch(Exception e) {};
}
if(isr != null) {
try { isr.close(); } catch(Exception e) {};
} else {
if(is != null) {
try { is.close(); } catch(Exception e) {};
}
}
if(connection != null) {
try { connection.disconnect(); } catch(Exception e) {};
}
}
}
a analogicky zápis je přes OutputStreamWriter
OutputStreamWriter osw = new OutputStreamWriter(os, responseEncoding);
osw.write(responseDataXml);
osw.flush();
Jaroslav Hurdes
Dne 7.12.2012 8:43, Ivan Polak napsal(a):
Zdravim konferenciu,
chcel som pouzit HttpClient z Apache HttpComponents Client
(http://hc.apache.org/httpcomponents-client-ga/) a ziskat obsah
jedneho webu, nasledovnym kodom:
static StringBuffer getRequest() {
StringBuffer result = new StringBuffer();
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://www.adresa.sk");
HttpParams params = new SyncBasicHttpParams();
HttpProtocolParams.setContentCharset(params, "UTF-8");
request.setParams(params);
try {
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream stream = entity.getContent();
try {
BufferedReader reader =
new BufferedReader(new InputStreamReader(stream));
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
} finally {
stream.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
vsetko je OK, ale ziskany obsah ma poskodenu diakritiku (cielovy web
je urcite v kodovani UTF-8).
prosim, neviete niekto poradit ako dosiahnut spravnu diakritiku.
dakujem
Ivan