Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7835489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:39:28+00:00 2026-06-02T13:39:28+00:00

I am trying to make an HTTPClient connection using sockets and can’t figure it

  • 0

I am trying to make an HTTPClient connection using sockets and can’t figure it out. When I run the code, I get the following message;

HTTP/1.1 400 Bad Request
Date: Sun, 22 Apr 2012 13:17:12 GMT
Server: Apache/2.2.16 (Debian)
Vary: Accept-Encoding
Content-Length: 317
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache/2.2.16 (Debian) Server at pvs.ifi.uni-heidelberg.de Port 80</address>
</body></html>

The code is here:

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

public class a1 {
  public static void main ( String[] args ) throws IOException {
    Socket s = null;

    try {
        String host = "host1";
        String file = "file1";
        int port = 80;

Here I am creating the socket:

         s = new Socket(host, port);

        OutputStream out = s.getOutputStream();
        PrintWriter outw = new PrintWriter(out, false);
        outw.print("GET " + file + " HTTP/1.1\r\n");
        outw.print("Accept: text/plain, text/html, text/*\r\n");
        outw.print("\r\n");
        outw.flush();           

        InputStream in = s.getInputStream();
        InputStreamReader inr = new InputStreamReader(in);
        BufferedReader br = new BufferedReader(inr);
        String line;

        while ((line = br.readLine()) != null) {
                System.out.println(line);
        }

    } 
    catch (UnknownHostException e) {} 
    catch (IOException e) {}

        if (s != null) {
                try {
                        s.close();
                }
                catch ( IOException ioEx ) {}
        }
  }
}   

Any help will be appreciated. Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T13:39:31+00:00Added an answer on June 2, 2026 at 1:39 pm

    If you really, really want to write your own HTTP client (harder than it seems, but very educative), you are missing the required Host header in HTTP 1.1:

    outw.print("Host: " + host + ":" + port + "\r\n");
    

    See RFC 2616, section 14.23. Host:

    A client MUST include a Host header field in all HTTP/1.1 request messages.

    So your request is bad, it misses required Host header.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Iam trying to make a connection between my database on the MYSQL(iam using the
I'm trying to make a utility httpClient class My code works great the first
I'm trying to make Https connections on the Android phones, using HttpClient. Since the
I am trying to get my code work. I want to make a mysql
I'm trying to make HTTPS connections, using HttpClient lib, but the problem is that,
I am trying to make Http connections made by C# HttpClient helper class reusable.
I'm trying to make a simple http client server using java. It will show
I am trying to make a get request from the GWT servlet to get
I am trying to make get requests by a servlet in my GWT application.
I am trying to make a secure connection to a OCS server through https

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.