Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Managing Http requests

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Thanked 0 Times in 0 Posts

    Default Managing Http requests

    Hello, i would like some enlightment with my project.
    I am trying to create a class that will be able to create and sent http requests (GET of POST). I also want it to be able to change some http headers and sent the request. I am a bit confused about the order in which the statements should be executed to have the results. For example i saw some tutorials put first open connection and then set the request type.
    I thought the order should be set request type,set headers, openconnection.
    I am using the HttpURLConnection.

    Here is what i have done so far:
    import java.net.*; import java.io.*; public class HTTP { private String - Pastebin.com
    import java.net.*;
    import java.io.*;
    public class HTTP {
            private String baseURL;
            private String path;
            private String fullPath;
            private HttpURLConnection http;
            public HTTP(String url){
            public void addHeaders(String[] headers,String values){
                    for(int i=0;i<headers.length;i++);
            public void changePath(String path){
            public static String getBaseUrl(String url){
                    String val="http://";
                    if(!url.contains("http://")) url="http://".concat(url);
                    String results[]=url.split("/");
                    if(results.length>=3) return val.concat(results[2]);
                    return "";
            private void openConnection(){
                    try {
                    URL u = new URL(this.fullPath);
                    this.http = (HttpURLConnection) u.openConnection();
                  }catch (MalformedURLException ex) {
                    System.err.println(this.fullPath + " is not a URL I understand");
                  }catch (IOException ex) {
            public void setHeader(String header,String value){
                    this.http.setRequestProperty(header, value);
            public void addHeader(String header,String value){
                    this.http.addRequestProperty(header, value);
            public void printHeaders(){
                    for (int j = 1; ; j++) {
                      String header = this.http.getHeaderField(j);
                      String key = this.http.getHeaderFieldKey(j);
                      if (header == null || key == null) break;
                      System.out.println(this.http.getHeaderFieldKey(j) + ": " + header);
            public static void main(String[] args) {
                            HTTP h=new HTTP("http://google.com/agsa");
                            h.setHeader("Referer", "wwwa.ga.a");
    Attached Files Attached Files

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,465 Times in 2,416 Posts

    Default Re: Managing http requests [help]

    Please post the code you are working on here in the forum. Be sure to wrap your code with
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Thanked 0 Times in 0 Posts

    Default Re: Managing Http requests

    I have found that using apache HttpClient might be easier.
    Thank you

Similar Threads

  1. Servlet to respond many requests
    By CarlosM87 in forum Java Servlet
    Replies: 3
    Last Post: January 31st, 2012, 04:18 AM
  2. Help Managing Generated Data
    By PineAppleKing in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 16th, 2011, 04:06 PM
  3. Managing an embedded database
    By jstn455 in forum JDBC & Databases
    Replies: 1
    Last Post: April 24th, 2011, 10:06 AM
  4. problem in managing layout
    By namreen in forum What's Wrong With My Code?
    Replies: 0
    Last Post: August 26th, 2010, 12:52 AM
  5. Tutorial requests
    By Json in forum The Cafe
    Replies: 9
    Last Post: August 1st, 2009, 03:14 PM