Results 1 to 5 of 5
  1. #1
    dhana is offline Member
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Default how to POST data to server using HttpURLConnection ??

    Hi friends,

    I have a problem to send a data using HttpURLConnection* can any one help me how to post a data.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,556
    Rep Power
    23

    Default

    Can you post your code and explain what it is doing and why that is wrong?

  3. #3
    dhana is offline Member
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Default how to POST data to server using HttpURLConnection ??

    Hi, i need to write HttpClient and HttpServer like a TCPClient and server
    server wants to listen a given port and IP and client post data to the server using HttpConnection, then server receives client data and show in a console.

  4. #4
    dhana is offline Member
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Default

    HttpServer*************************
    import java.io.IOException;
    import java.io.OutputStream;
    import java.net.InetSocketAddress;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Set;
    import java.util.concurrent.Executors;
    import com.sun.net.httpserver.Headers;
    import com.sun.net.httpserver.HttpExchange;
    import com.sun.net.httpserver.HttpHandler;
    import com.sun.net.httpserver.HttpServer;

    public class Httpserver implements HttpHandler {
    public static void main(String[] args) {
    try {
    InetSocketAddress addr = new InetSocketAddress(8080);
    System.out.println("InetSocketAddress " + addr);
    HttpServer server = HttpServer.create(addr, 0);
    System.out.println("HttpServer " + server);
    server.createContext("/", new Httpserver());
    System.out.println("Server creating context ");
    server.setExecutor(Executors.newCachedThreadPool() );
    server.start();
    System.out.println("Server is listening on port 8080");

    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    public void handle(HttpExchange exchange) throws IOException {
    // TODO Auto-generated method stub

    System.out.println("MyHandler ");
    String requestMethod = exchange.getRequestMethod();
    if (requestMethod.equalsIgnoreCase("POST")) {
    Headers responseHeaders = exchange.getResponseHeaders();
    System.out.println("Get Response Headers "+responseHeaders);
    responseHeaders.set("Content-Type", "text/plain");
    exchange.sendResponseHeaders(200, 0);

    OutputStream responseBody = exchange.getResponseBody();
    System.out.println("Outputstream response body "+responseBody);
    Headers requestHeaders = exchange.getRequestHeaders();
    Set<String> keySet = requestHeaders.keySet();
    System.out.println("Set value "+keySet);
    Iterator<String> iter = keySet.iterator();
    while (iter.hasNext()) {
    String key = iter.next();
    List values = requestHeaders.get(key);
    String s = key + " = " + values.toString() + "\n";
    responseBody.write(s.getBytes());
    System.out.println(s);
    System.out.print(s.toString());
    }
    responseBody.close();
    }

    }
    }

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,556
    Rep Power
    23

Similar Threads

  1. Replies: 0
    Last Post: 09-01-2008, 11:57 AM
  2. First post ever
    By pbpersson in forum Introductions
    Replies: 4
    Last Post: 08-16-2008, 05:30 AM
  3. The first post and first question
    By m_kaleia in forum New To Java
    Replies: 8
    Last Post: 07-19-2008, 06:27 PM
  4. Problem with NTLM ,data not posting to the server
    By raviprakash in forum JavaServer Faces (JSF)
    Replies: 3
    Last Post: 01-03-2008, 03:33 PM
  5. Replies: 1
    Last Post: 07-14-2007, 05:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •