basic java net client returns 400












0














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.










share|improve this question


















  • 1




    The line terminator in HTTP is defined as rn, not just n.
    – user207421
    Nov 23 '18 at 7:04
















0














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.










share|improve this question


















  • 1




    The line terminator in HTTP is defined as rn, not just n.
    – user207421
    Nov 23 '18 at 7:04














0












0








0







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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 6:59









Alexander McNulty

349214




349214








  • 1




    The line terminator in HTTP is defined as rn, not just n.
    – user207421
    Nov 23 '18 at 7:04














  • 1




    The line terminator in HTTP is defined as rn, not just n.
    – 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












1 Answer
1






active

oldest

votes


















1














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?






share|improve this answer





















  • 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











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
});


}
});














draft saved

draft discarded


















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









1














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?






share|improve this answer





















  • 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
















1














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?






share|improve this answer





















  • 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














1












1








1






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?






share|improve this answer












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?







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 7:09









drowny

1,605314




1,605314












  • 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










  • 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


















draft saved

draft discarded




















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Trompette piccolo

Slow SSRS Report in dynamic grouping and multiple parameters

Simon Yates (cyclisme)