Results 1 to 6 of 6

Thread: Send Http Request In Java

  1. #1
    Join Date
    Nov 2009
    Posts
    117

    Send Http Request In Java

    Hello, I want to know the details about the Sending Http Request In Java. I have search regarding this on internet, but I am not able to get the solution on it. If you are having more details about it, then please help me to achieve it. I will be thankful to you.

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: Send Http Request In Java

    Hello, HttpRequest is one class in the java. It is having following methos which can be used by the user in your code:
    • addHeader(java.lang.String line)
    • close()
    • getHeader(java.lang.String string)
    • getInputStream()
    • getMethod()
    • getRequestURI()
    • pushBack(byte[] buf, int start, int length)
    • setContentLength(int contentLength)

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Send Http Request In Java

    Hello, you can simply make use of the code below for getting the solution for your problem:
    Code:
    String file_name = “test.html”;
    URL url = new URL(”http://193.20.30.20/target_path”);
    FileWriter filewriter = new FileWriter(file_name);
    postData(new StringReader(xml_data), url, filewriter);

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Send Http Request In Java

    Hello, I have the code below for sending the HTTP Request in java. You can simply make use of it and get your problem solved:
    Code:
    package HTTPUtil;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.io.Reader;
    import java.io.Writer;
    import java.net.HttpURLConnection;
    import java.net.ProtocolException;
    import java.net.URL;
    import java.net.URLConnection;
    public class HTTPRequestPoster
    {
    public static String HttpRequest(String target, String reqpar)
    {
    String result = null;
    if (target.startsWith("http://"))
    {
    try
    {
    StringBuffer sbufferfer = new StringBuffer();
    String urlString = target;
    if (reqpar != null && reqpar.length () > 0)
    {
    urlString += "?" + reqpar;
    }
    URL url = new URL(urlString);
    URLConnection conn = url.openConnection ();
    BufferedReader bufferferreader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    StringBuffer sb = new StringBuffer();
    String line;
    while ((line = bufferferreader.readLine()) != null)
    {
    sb.append(line);
    }
    bufferferreader.close();
    result = sb.toString();
    } catch (Exception e)
    {
    e.printStackTrace();
    }
    }
    return result;
    }
    public static void postData(Reader sbufferfer, URL target, Writer output) throws Exception
    {
    HttpURLConnection httpurlcon = null;
    try
    {
    httpurlcon = (HttpURLConnection) target.openConnection();
    try
    {
    httpurlcon.setRequestMethod("POST");
    } catch (ProtocolException e)
    {
    throw new Exception("Shouldn't happen: HttpURLConnection doesn't support POST??", e);
    }
    httpurlcon.setDoOutput(true);
    httpurlcon.setDoInput(true);
    httpurlcon.setUseCaches(false);
    httpurlcon.setAllowUserInteraction(false);
    httpurlcon.setRequestProperty("Content-type", "text/xml; charset=" + "UTF-8");
    OutputStream out = httpurlcon.getOutputStream();
    try
    {
    Writer writer = new OutputStreamWriter(out, "UTF-8");
    pipe(sbufferfer, writer);
    writer.close();
    } catch (IOException e)
    {
    throw new Exception("IOException while posting sbufferfer", e);
    } finally
    {
    if (out != null)
    out.close();
    }
    InputStream in = httpurlcon.getInputStream();
    try
    {
    Reader reader = new InputStreamReader(in);
    pipe(reader, output);
    reader.close();
    } catch (IOException e)
    {
    throw new Exception("IOException while reading response", e);
    } finally
    {
    if (in != null)
    in.close();
    }
    } catch (IOException e)
    {
    throw new Exception("Connection error (is server running at " + target + " ?): " + e);
    } 
    finally
    {
    if (httpurlcon != null)
    httpurlcon.disconnect();
    }
    }
    private static void pipe(Reader reader, Writer writer) throws IOException
    {
    char[] buffer = new char[1024];
    int read = 0;
    while ((read = reader.read(buffer)) >= 0)
    {
    writer.write(buffer, 0, read);
    }
    writer.flush();
    }
    }

  5. #5
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Send Http Request In Java

    For sending HTTP request you can simply make use of the code below:
    Code:
    URL url = new URL("http://www.techarena.in/url");
    InputStream iStream = url.openStream();
    try {
      ...
    } finally 
    {
      iStream.close();
    }

  6. #6
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Send Http Request In Java

    If you want to make use of the java for sending http request in java then you need to understand the code below:
    Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.URL;
     
    public class HTTPTesting {
        public static void main(String[] args) {
            String reqUrl = "http://www.TA.in";
            try {
                URL url = new URL(reqUrl.toString());
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()));
                String iLine;
                System.out.println("Started For the Response");
                while ((iLine = bufferedReader.readLine()) != null) {
                    System.out.println(iLine);
                }
                bufferedReader.close();
                System.out.println("Completion of Response");
                
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

Similar Threads

  1. Replies: 5
    Last Post: 18-02-2011, 10:07 PM
  2. How to send HTTP request from specific IP?
    By ANSEL in forum Technology & Internet
    Replies: 4
    Last Post: 28-01-2010, 08:20 PM
  3. HTTP request attributes
    By KAIRU26 in forum Software Development
    Replies: 3
    Last Post: 24-11-2009, 05:59 PM
  4. How to fix http 400 bad request
    By Beans in forum Technology & Internet
    Replies: 3
    Last Post: 22-08-2009, 05:32 PM
  5. HTTP 400 Bad Request
    By adyf in forum Vista Help
    Replies: 2
    Last Post: 04-11-2008, 02:33 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,002,541.25031 seconds with 17 queries