Java download html - java

I am trying do download the html of a website:
String encoding = "UTF-8";
HttpContext localContext = new BasicHttpContext();
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(MYURL);
httpget.setHeader("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3");
HttpResponse response = httpclient.execute(httpget, localContext);
HttpEntity entity = response.getEntity();
InputStream instream = entity.getContent();
String html = getStringFromInputStream(encoding, instream);
And in the and of the html string i get:
...
21912
0
0
And i don't get the full html,any idea how to fix?
EDIT
private static String getStringFromInputStream(String encoding, InputStream instream) throws UnsupportedEncodingException, IOException {
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(instream, encoding));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
instream.close();
}
String result = writer.toString();
return result;
}

I would suggest rather use EntityUtils:
HttpEntity entity = response.getEntity();
String html = EntityUtils.toString(entity);
or
HttpEntity entity = response.getEntity();
String html = EntityUtils.toString(entity, encoding);

Related

About HTTP Server,The client receives the data that is not returned by the server

This is Server and Client code,Please help me to see where the error,It can be post data to the server but httpResponse not received,Tell me why,Thanks!
imagcode: POST
public void dopost(){
HttpClient httpClient = new DefaultHttpClient();
try {
HttpPost request = new HttpPost("http://127.0.0.1:8088");
StringEntity params =new StringEntity("{\"username\":\"De\",\"passwd\":\"REsfg7ufghfgh\",\"public\":\"ERTYU45646\"} ");
request.addHeader("content-type", "application/json");
request.addHeader("Accept","application/json");
request.addHeader(new BasicHeader("Cookie","JSESSIONID=B6FF25530B16AB46CA77B08129FECFB3"));
request.setEntity(params);
HttpResponse httpResponse = httpClient.execute(request);
clientread();
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(httpEntity.getContent(),"UTF-8"), 8 * 1024);
StringBuilder entityStringBuilder = new StringBuilder();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
entityStringBuilder.append(line + "/n");
}
System.out.println(entityStringBuilder.toString());
JSONObject resultJsonObject = new JSONObject(entityStringBuilder.toString());
System.out.println(resultJsonObject.toString());
}
}
}catch (Exception e) {
throw new RuntimeException(e);
} finally {
httpClient.getConnectionManager().shutdown();
}
}
some server code :SERVER2
Server code is:
else if (key.isWritable()) {
SocketChannel channel = (SocketChannel) key.channel();
JSONObject res1 = new JSONObject();
res1.put("result","OK");
ByteBuffer buffer = ByteBuffer.allocate(1024);
byte[] bytes = res1.toString().getBytes();
buffer.put(bytes);
buffer.flip();
channel.write(buffer);
channel.shutdownInput();
channel.close();
}

Download PDF using Apache HttpClient

I'm writing a program to download all of my monthly statements from my ISP using HttpClient. I can login to the site, access pages, and download pages but I can't download my PDF statements. It just downloads some HTML. I used the answer to this question to start with. Here is my method where I'm trying to download the PDF:
public void downloadPdf() throws ClientProtocolException, IOException {
HttpGet httpget = new HttpGet("https://www.cox.com/ibill/PdfBillingStatement.stmt?account13=123&stmtCode=001&cycleDate=7/21/2014&redirectURL=error.cox");
HttpResponse response = client.execute(httpget);
System.out.println("Download response: " + response.getStatusLine());
HttpEntity entity = response.getEntity();
InputStream inputStream = null;
OutputStream outputStream = null;
if (entity != null) {
long len = entity.getContentLength();
inputStream = entity.getContent();
outputStream = new FileOutputStream(new File("/home/bkurczynski/Desktop/statement.pdf"));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
outputStream.close();
}
}
Any help would be greatly appreciated. Thank you!
HttpClient httpClient = HttpClientBuilder.create().build();
try {
HttpGet request = new HttpGet("https://www.cox.com/ibill/PdfBillingStatement.stmt?account13=123&stmtCode=001&cycleDate=7/21/2014&redirectURL=error.cox");
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
String filePath = "hellow.txt";
FileOutputStream fos = new FileOutputStream(new File(filePath));
int inByte;
while ((inByte = is.read()) != -1)
fos.write(inByte);
is.close();
fos.close();
} catch (Exception ex) {
}

generate byte array from StringBuffer.toString

What I'm trying to do is to generate a byte array from a url.
byte[] data = WebServiceClient.download(url);
The url returns json
public static byte[] download(String url) {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
try {
HttpResponse response = client.execute(get);
StatusLine status = response.getStatusLine();
int code = status.getStatusCode();
switch (code) {
case 200:
StringBuffer sb = new StringBuffer();
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
is.close();
sContent = sb.toString();
break;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sContent.getBytes();
}
This data is used as a parameter for String
String json = new String(data, "UTF-8");
JSONObject obj = new JSONObject(json);
for some reason, I get this error
I/global ( 631): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
I think something there must be missing here sContent = sb.toString(); or here return sContent.getBytes(); but I'm not sure though.
1. Consider using Apache commons-io to read the bytes from InputStream
InputStream is = entity.getContent();
try {
return IOUtils.toByteArray(is);
}finally{
is.close();
}
Currently you're unnecessarily converting the bytes to characters and back.
2. Avoid using String.getBytes() without passing the charset as a parameter. Instead use
String s = ...;
s.getBytes("utf-8")
As a whole I'd rewrite you're method like this:
public static byte[] download(String url) throws IOException {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
HttpResponse response = client.execute(get);
StatusLine status = response.getStatusLine();
int code = status.getStatusCode();
if(code != 200) {
throw new IOException(code+" response received.");
}
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
try {
return IOUtils.toByteArray(is);
}finally{
IOUtils.closeQuietly(is.close());
}
}

Call POST data from one servlet to another server

I am submitting JSON data from my GWT-Client and Passing it to my GWT-Server. And i want to re-submit data to another server and want response from another server to GWT-Client.
I just don't know how can i do this. I tried below code but not working.
My Code is :
#Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("POST");
StringBuffer jb = new StringBuffer();
URL oracle = new URL("http://www.google.com");
HttpURLConnection connection = null;
connection = (HttpURLConnection) oracle.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setDoInput(true);
request.getInputStream();
OutputStream wr = connection.getOutputStream();
InputStream in = request.getInputStream();
byte[] buffer = new byte[512];
int read = in.read(buffer, 0, buffer.length);
while (read >= 0) {
wr.write(buffer, 0, read);
read = in.read(buffer, 0, buffer.length);
}
wr.flush();
wr.close();
BufferedReader in1 = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String inputLine;
while ((inputLine = in1.readLine()) != null) {
jb.append(inputLine);
}
response.setContentType("text/html");
// Get the printwriter object from response to write the required json
// object to the output stream
PrintWriter out = response.getWriter();
// Assuming your json object is **jsonObject**, perform the following,
// it will return your json object
out.print(jb.toString());
out.flush();
in1.close();
}
Please help me.
You'll have to send the request to the other server before reading the response
URL oracle = new URL("http://www.anotherserver.com/");
HttpURLConnection connection = null;
connection = (HttpURLConnection) oracle.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
OutputStream wr = connection.getOutputStream ();
InputStream in = request.getInputStream();
byte[] buffer = new byte[512];
int read = in.read(buffer,0, buffer.length);
while (read >= 0) {
wr.write(buffer,0, read);
read = in.read(buffer,0,buffer.length);
}
wr.flush ();
wr.close ();
BufferedReader in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
jb.append(inputLine);
}
see also Using java.net.URLConnection to fire and handle HTTP requests
In addition, you can use Apache httpclient, it's very simple to implement your requirement, such as :
public static void main(String[] args) {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://www.anotherserver.com/");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("key",
"value"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}

Can I upload Images AND text using UrlEncodedFormEntity for multi part?

Images often requires special HTTP headers like:
Content-disposition: attachment; filename="file2.jpeg"
Content-type: image/jpeg
Content-Transfer-Encoding: binary
I'm building my POST using:
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(formparams);
urlEncodedFormEntity allows setContentType, but I don't see how I can MIX both images and text ??
try {
File file = new File(Environment.getExternalStorageDirectory(),"FMS_photo.jpg");
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://homepage.com/path");
FileBody bin = new FileBody(file);
Charset chars = Charset.forName("UTF-8");
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("problem[photos_attributes][0][image]", bin);
reqEntity.addPart("myString", new StringBody("17", chars));
post.setEntity(reqEntity);
HttpResponse response = client.execute(post);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
resEntity.consumeContent();
}
return true;
} catch (Exception ex) {
globalStatus = UPLOAD_ERROR;
serverResponse = "";
return false;
} finally {
}
in this the problem attribute will carry the image and myString carry the string...

Categories

Resources