Here you can find the source of requestDataFromUrl(URL url, byte[] tosend, String userAgent)
public static StringBuilder requestDataFromUrl(URL url, byte[] tosend, String userAgent) throws IOException
//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; } }