Hello! There is such a program
import java.io.*; import java.net.Socket; import java.text.ParseException; import static java.lang.Thread.sleep; public class HttpClient { public static void main(String[] args) { try { String header = null; if (args.length == 0) { header = readHeader(System.in); } else { FileInputStream fis = new FileInputStream(args[0]); header = readHeader(fis); fis.close(); } if (header.endsWith("\n")) header+='\n'; //System.out.println("Заголовок: \n" + header); /* Запрос отправляется на сервер и мы получаем ответ*/ String answer = sendRequest(header); /* Ответ выводится на консоль */ System.out.println("Ответ от сервера: \n"); System.out.write(answer.getBytes()); } catch (Exception e) { System.err.println(e.getMessage()); e.getCause().printStackTrace(); } } /** * Читает поток и возвращает его содержимое в виде строки. */ public static String readHeader(InputStream strm) throws IOException { byte[] buff = new byte[64 * 1024]; int length = strm.read(buff); String res = new String(buff, 0, length); return res; } /** * Отправляет запрос в соответствии с Http заголовком. * * @return ответ от сервера. */ public static String sendRequest(String httpHeader) throws Exception { /* Из http заголовка берется арес сервера */ String host = null; int port = 0; try { host = getHost(httpHeader); port = getPort(host); host = getHostWithoutPort(host); } catch (Exception e) { throw new Exception("Не удалось получить адрес сервера.", e); } /* Отправляется запрос на сервер */ Socket socket; try { socket = new Socket(host, port); System.out.println("Создан сокет: " + host + " port:" + port); OutputStream os = socket.getOutputStream(); os.write(httpHeader.getBytes()); os.flush(); System.out.println("Заголовок отправлен. \n"); } catch (Exception e) { throw new Exception("Ошибка при отправке запроса: " + e.getMessage(), e); } /* Ответ от сервера записывается в результирующую строку */ String res = null; try { BufferedReader bfr = new BufferedReader(new InputStreamReader(socket.getInputStream())); StringBuffer sbf = new StringBuffer(); int ch = bfr.read(); while (ch != -1) { sbf.append((char) ch); ch = bfr.read(); } res = sbf.toString(); } catch (Exception e) { throw new Exception("Ошибка при чтении ответа от сервера.", e); } socket.close(); return res; } /** * Возвращает имя хоста (при наличии порта, с портом) из http заголовка. */ private static String getHost(String header) throws ParseException { final String host = "Host: "; final String normalEnd = "\n"; final String msEnd = "\r\n"; int s = header.indexOf(host, 0); if (s < 0) { return "localhost"; } s += host.length(); int e = header.indexOf(normalEnd, s); e = (e > 0) ? e : header.indexOf(msEnd, s); if (e < 0) { throw new ParseException( "В заголовке запроса не найдено " + "закрывающих символов после пункта Host.", 0); } String res = header.substring(s, e).trim(); return res; } /** * Возвращает номер порта. */ private static int getPort(String hostWithPort) { int port = hostWithPort.indexOf(":", 0); port = (port < 0) ? 80 : Integer.parseInt(hostWithPort .substring(port + 1)); return port; } /** * Возвращает имя хоста без порта. */ private static String getHostWithoutPort(String hostWithPort) { int portPosition = hostWithPort.indexOf(":", 0); if (portPosition < 0) { return hostWithPort; } else { return hostWithPort.substring(0, portPosition); } } } Sending a post request
GET https://api.telegram.org/bot294573807:A213aV0Ggc_ouJc-DbMsVhJzhnXqzWuBZ8w/getupdates HTTP/1.1 Host: api.telegram.org User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.0.3705) I get this answer
<html> <head><title>301 Moved Permanently</title></head> <body bgcolor="white"> <center><h1>301 Moved Permanently</h1></center> <hr><center>nginx/1.10.0</center> </body> </html> How to redirect?
HTTP/1.1 301 Moved Permanently Location: http://www.example.org/ Content-Type: text/html ...- SergeyHTTP/1.1 301 Moved Permanently Server: nginx/1.10.0 Date: Thu, 22 Dec 2016 03:12:52 GMT Content-Type: text/html Content-Length: 185 Connection: keep-alive Location: https://api.telegram.org/bot294573807:AAGEaV0Ggc_ouJc-DbMsVhJzhnXqzWuBZ8w/getupdateslink - Andru301, extract the address from theLocationheader field and make a new request to this address (https://api.telegram.org/bot294573807:AAGEaV0Ggc_ouJc-DbMsVhJzhnXqzWuBZ8w/getupdatesin your case). Additionally, since it isMoved permanently, remember this redirection and the following requests immediately send immediately to the new address. It seems that it is desirable to do so withMoved permanently. - Sergey