URLConnection getOutputStream Example

URLConnection class getOutputStream example. This example shows you how to use getOutputStream method.


URLConnection class getOutputStream example. public OutputStream getOutputStream() throws IOException Returns an output stream that writes to this connection.

Here is the code
/*
 * @ # GetOutputStream.java
 * A class repersenting use to GetOutputStream method 
 * of URL class in java.net package
 * version 24 June 2008
 * author Rose India 
 */

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

public class GetOutputStream {

    public static void main(String args[]) throws Exception {

        SocketAddress socketAddress =
                new InetSocketAddress("192.168.10.80"9090);
        Proxy proxy = new Proxy(Proxy.Type.HTTP, socketAddress);

        URL url = new URL("http://google.com");
        URLConnection connection = url.openConnection(proxy);

        connection.setDoOutput(true);
        connection.setDoInput(true);

        String s = new String("Hi, this is komal");
        byte[] b = s.getBytes();

        DataOutputStream outputStream =
                new DataOutputStream(connection.getOutputStream());
        outputStream.write(b);
        outputStream.flush();
        outputStream.close();

        DataInputStream inputStream =
                new DataInputStream(connection.getInputStream());
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        while (br.ready()) {
            System.out.println(br.readLine());
        }
        inputStream.close();
    }
}

Output
Exception in thread "main" java.io.IOException:
Server returned HTTP response code: 501 for URL: http://google.com at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1241)
at GetOutputStream.main(GetOutputStream.java:35)

Post Comment
Name:
E-mail:
Contact no :
Comments:
  Refresh Image
Verify Image:
 
 
Your Comment's
 
 
 

HOME | COPYRIGHT | CONTACT US