웹서버에게 요청을 웹브라우저가 아닌 프로그램으로 해야될 경우가 있습니다.
그럴때 사용하는 프로그램에 대한 소스입니다. (Raw Socket)

import java.net.*;
import java.io.*;

class raw_socket
{
        public static void main(String[] args)
        {
               try {
                 // Construct data
                 String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
                 data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");
    
                 // Create a socket to the host
                 String hostname = "";
                 int port = 0;
                 InetAddress addr = InetAddress.getByName(hostname);
                 Socket socket = new Socket(addr, port);
     
                 // Send header
                 String path = "";          //세부주소.
   
                BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
                wr.write("POST "+path+" HTTP/1.0\r\n");
                wr.write("Content-Length: "+data.length()+"\r\n");
                wr.write("Content-Type: application/x-www-form-urlencoded\r\n");
                wr.write("\r\n");

                // Send data
                wr.write(data);
                wr.flush();
   
                // Get response
               BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
               String line;
               while ((line = rd.readLine())!= null) {
                      // Process line...
               }
   
               wr.close();
               rd.close();
        }
        catch (Exception e) {
        System.out.println("error");
        }
    }
}

'Network > _Network Programming' 카테고리의 다른 글

C로짠 Client Windows Socket 소스  (0) 2010.11.15
Java 로 짠 Raw Socket 소스  (0) 2010.11.15
리눅스 소켓 예제소스  (0) 2010.03.30
Posted by Dakuo