Java HTTP Request requestDataFromUrl(URL url, byte[] tosend, String userAgent)

Here you can find the source of requestDataFromUrl(URL url, byte[] tosend, String userAgent)

Description

request Data From Url

License

Open Source License

Declaration

public static StringBuilder requestDataFromUrl(URL url, byte[] tosend, String userAgent) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    public static StringBuilder requestDataFromUrl(URL url, byte[] tosend, String userAgent) throws IOException {
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        if (userAgent != null) {
            urlConnection.setRequestProperty("User-Agent", userAgent);
        }//  ww w . ja  v a2 s .c  o m
        urlConnection.setDoOutput(true);
        urlConnection.setDoInput(true);
        urlConnection.setConnectTimeout(5000);
        urlConnection.setReadTimeout(20000);
        // urlConnection.setChunkedStreamingMode(0);
        urlConnection.setFixedLengthStreamingMode(tosend.length);

        BufferedOutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
        out.write(tosend);
        out.flush();
        out.close();

        InputStreamReader reader = new InputStreamReader(urlConnection.getInputStream());
        StringBuilder sb = new StringBuilder();
        char[] buf = new char[2048];
        int i;
        while ((i = reader.read(buf)) != -1) {
            sb.append(buf, 0, i);
        }
        return sb;
    }
}

Related

  1. request(boolean quiet, String method, URL url, Map body)
  2. request(String httpUrl, Map httpArgMap)
  3. request(String url, int timeout, String method, Map header)
  4. request(String url, Map cookies, Map parameters)
  5. requestData(String url)
  6. saveHttpImage(String requestUrl, String requestMethod, String outputStr, File target)
  7. sendGet(String url)
  8. sendGet(String url, String param)
  9. sendGetRequest(String requestURL)