basic java net client returns 400
The following code has been copied out for Horstmann's, Big Java Early Objects 6th edition - Chapter 23.3. The only modification is the package, added by eclipse.
The code:
package zipCodeScrapper;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class client {
public static void main(String args) throws IOException {
// Get command-line arguments
String host;
String resource;
if (args.length == 2) {
host = args[0];
resource = args[1];
} else {
System.out.println("Getting / from horstmann.com");
host = "horstmann.com";
resource = "/";
}
// Open socket
final int HTTP_PORT = 80;
try (Socket s = new Socket(host, HTTP_PORT)) {
// Get streams
InputStream instream = s.getInputStream();
OutputStream outstream = s.getOutputStream();
// Turn streams into scanners and writers
Scanner in = new Scanner(instream);
PrintWriter out = new PrintWriter(outstream);
// Send command
String command = "GET " + resource + " HTTP/1.1n" + "Host: " + host + "nn";
out.print(command);
out.flush();
while (in.hasNextLine()) {
String input = in.nextLine();
System.out.println(input);
}
}
}
}
The code receives a 400 Bad Request. I have tried replacing the host and resources with other values, however I continue to get error 400.
java sockets http-status-code-400
add a comment |
The following code has been copied out for Horstmann's, Big Java Early Objects 6th edition - Chapter 23.3. The only modification is the package, added by eclipse.
The code:
package zipCodeScrapper;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class client {
public static void main(String args) throws IOException {
// Get command-line arguments
String host;
String resource;
if (args.length == 2) {
host = args[0];
resource = args[1];
} else {
System.out.println("Getting / from horstmann.com");
host = "horstmann.com";
resource = "/";
}
// Open socket
final int HTTP_PORT = 80;
try (Socket s = new Socket(host, HTTP_PORT)) {
// Get streams
InputStream instream = s.getInputStream();
OutputStream outstream = s.getOutputStream();
// Turn streams into scanners and writers
Scanner in = new Scanner(instream);
PrintWriter out = new PrintWriter(outstream);
// Send command
String command = "GET " + resource + " HTTP/1.1n" + "Host: " + host + "nn";
out.print(command);
out.flush();
while (in.hasNextLine()) {
String input = in.nextLine();
System.out.println(input);
}
}
}
}
The code receives a 400 Bad Request. I have tried replacing the host and resources with other values, however I continue to get error 400.
java sockets http-status-code-400
1
The line terminator in HTTP is defined asrn
, not justn
.
– user207421
Nov 23 '18 at 7:04
add a comment |
The following code has been copied out for Horstmann's, Big Java Early Objects 6th edition - Chapter 23.3. The only modification is the package, added by eclipse.
The code:
package zipCodeScrapper;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class client {
public static void main(String args) throws IOException {
// Get command-line arguments
String host;
String resource;
if (args.length == 2) {
host = args[0];
resource = args[1];
} else {
System.out.println("Getting / from horstmann.com");
host = "horstmann.com";
resource = "/";
}
// Open socket
final int HTTP_PORT = 80;
try (Socket s = new Socket(host, HTTP_PORT)) {
// Get streams
InputStream instream = s.getInputStream();
OutputStream outstream = s.getOutputStream();
// Turn streams into scanners and writers
Scanner in = new Scanner(instream);
PrintWriter out = new PrintWriter(outstream);
// Send command
String command = "GET " + resource + " HTTP/1.1n" + "Host: " + host + "nn";
out.print(command);
out.flush();
while (in.hasNextLine()) {
String input = in.nextLine();
System.out.println(input);
}
}
}
}
The code receives a 400 Bad Request. I have tried replacing the host and resources with other values, however I continue to get error 400.
java sockets http-status-code-400
The following code has been copied out for Horstmann's, Big Java Early Objects 6th edition - Chapter 23.3. The only modification is the package, added by eclipse.
The code:
package zipCodeScrapper;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class client {
public static void main(String args) throws IOException {
// Get command-line arguments
String host;
String resource;
if (args.length == 2) {
host = args[0];
resource = args[1];
} else {
System.out.println("Getting / from horstmann.com");
host = "horstmann.com";
resource = "/";
}
// Open socket
final int HTTP_PORT = 80;
try (Socket s = new Socket(host, HTTP_PORT)) {
// Get streams
InputStream instream = s.getInputStream();
OutputStream outstream = s.getOutputStream();
// Turn streams into scanners and writers
Scanner in = new Scanner(instream);
PrintWriter out = new PrintWriter(outstream);
// Send command
String command = "GET " + resource + " HTTP/1.1n" + "Host: " + host + "nn";
out.print(command);
out.flush();
while (in.hasNextLine()) {
String input = in.nextLine();
System.out.println(input);
}
}
}
}
The code receives a 400 Bad Request. I have tried replacing the host and resources with other values, however I continue to get error 400.
java sockets http-status-code-400
java sockets http-status-code-400
asked Nov 23 '18 at 6:59
Alexander McNultyAlexander McNulty
384214
384214
1
The line terminator in HTTP is defined asrn
, not justn
.
– user207421
Nov 23 '18 at 7:04
add a comment |
1
The line terminator in HTTP is defined asrn
, not justn
.
– user207421
Nov 23 '18 at 7:04
1
1
The line terminator in HTTP is defined as
rn
, not just n
.– user207421
Nov 23 '18 at 7:04
The line terminator in HTTP is defined as
rn
, not just n
.– user207421
Nov 23 '18 at 7:04
add a comment |
1 Answer
1
active
oldest
votes
Firstly , read this documentations. RFC p1 and RFC2 p2
Line separator of HTTP is not a n
. It must be rn
. For more read this thread ;
What is line breaker in HTTP?
in case this isn't clear to anyone who has the same problem, every n in my "command" string should be replace by rn
– Alexander McNulty
Nov 23 '18 at 7:21
thanks for the answer, and +1 for the suggested reading.
– Alexander McNulty
Nov 23 '18 at 7:22
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441974%2fbasic-java-net-client-returns-400%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Firstly , read this documentations. RFC p1 and RFC2 p2
Line separator of HTTP is not a n
. It must be rn
. For more read this thread ;
What is line breaker in HTTP?
in case this isn't clear to anyone who has the same problem, every n in my "command" string should be replace by rn
– Alexander McNulty
Nov 23 '18 at 7:21
thanks for the answer, and +1 for the suggested reading.
– Alexander McNulty
Nov 23 '18 at 7:22
add a comment |
Firstly , read this documentations. RFC p1 and RFC2 p2
Line separator of HTTP is not a n
. It must be rn
. For more read this thread ;
What is line breaker in HTTP?
in case this isn't clear to anyone who has the same problem, every n in my "command" string should be replace by rn
– Alexander McNulty
Nov 23 '18 at 7:21
thanks for the answer, and +1 for the suggested reading.
– Alexander McNulty
Nov 23 '18 at 7:22
add a comment |
Firstly , read this documentations. RFC p1 and RFC2 p2
Line separator of HTTP is not a n
. It must be rn
. For more read this thread ;
What is line breaker in HTTP?
Firstly , read this documentations. RFC p1 and RFC2 p2
Line separator of HTTP is not a n
. It must be rn
. For more read this thread ;
What is line breaker in HTTP?
answered Nov 23 '18 at 7:09
drownydrowny
1,618414
1,618414
in case this isn't clear to anyone who has the same problem, every n in my "command" string should be replace by rn
– Alexander McNulty
Nov 23 '18 at 7:21
thanks for the answer, and +1 for the suggested reading.
– Alexander McNulty
Nov 23 '18 at 7:22
add a comment |
in case this isn't clear to anyone who has the same problem, every n in my "command" string should be replace by rn
– Alexander McNulty
Nov 23 '18 at 7:21
thanks for the answer, and +1 for the suggested reading.
– Alexander McNulty
Nov 23 '18 at 7:22
in case this isn't clear to anyone who has the same problem, every n in my "command" string should be replace by rn
– Alexander McNulty
Nov 23 '18 at 7:21
in case this isn't clear to anyone who has the same problem, every n in my "command" string should be replace by rn
– Alexander McNulty
Nov 23 '18 at 7:21
thanks for the answer, and +1 for the suggested reading.
– Alexander McNulty
Nov 23 '18 at 7:22
thanks for the answer, and +1 for the suggested reading.
– Alexander McNulty
Nov 23 '18 at 7:22
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441974%2fbasic-java-net-client-returns-400%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
The line terminator in HTTP is defined as
rn
, not justn
.– user207421
Nov 23 '18 at 7:04