나는 지금까지 내가 이런 짓을, HTTP server.I'm 소켓을 사용하여 내장 한 일부 응용 프로그램에 HTTP 요청을 사용하여 몇 가지 명령을 보내야하지만 난 조금 잃었어요 :HTTP 요청을 사용하여 소켓
URI uri = URI.create(rawData);
try {
String host = uri.getHost();
String path = uri.getRawPath();
if (path == null || path.length() == 0) {
path = "/";
}
String protocol = uri.getScheme();
int port = uri.getPort();
if (port == -1) {
if (protocol.equals("http")) {
port = 80; // http port
}
else if (protocol.equals("https")) {
port = 443; // https port
}
}
Socket socket = new Socket(host, port);
PrintWriter request = new PrintWriter(socket.getOutputStream());
request.print( "GET " + path + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
request.flush();
내가 할 일은 이것 뿐이니? rawData의 예는 "http://somemessage"
입니다. 올바르게 작성된 프로토콜입니까? 감사합니다.
URLConnection을 사용하지 않는 특별한 이유가 있습니까? – AlexR
@AlexR 필자가 고려해야 할 유일한 사항은 지금까지 브라우저 탭을 열지 않고이 작업을 수행하는 것입니다. "Desktop.getDesktop(). browse (URI.create (rawData)) ; " –