HTTP in Java socket


HTTP in Java socket



I want to send a JSON to an already written server (not from me).
My problem is that I have an HTTP problem and need to convert it to HTTP.



Server message:


2018-06-29 20:02:30.532:WARN:oejh.HttpParser:qtp245475541-15: badMessage: 400
No URI for HttpChannelOverHttp@2bb4ec27{r=0,c=false,a=IDLE,uri=}
2018-06-29 20:03:24.206:WARN:oejh.HttpParser:qtp245475541-18: badMessage: 400
No URI for HttpChannelOverHttp@866a0c9{r=0,c=false,a=IDLE,uri=}



Code:


public class client {

public static void main(String args) {
JsonObject js = new JsonObject();
js.addProperty("action","auth");
js.addProperty("type","android");
js.addProperty("token","lalalalalalalalala");
try {
Socket ss = new Socket("localhost",3000);
PrintWriter out =
new PrintWriter(ss.getOutputStream(), true);
BufferedReader in =
new BufferedReader(
new InputStreamReader(ss.getInputStream()));
out.println(js.toString());// sending json

System.out.println(in.readLine()); //recieving answer

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}



Output on console: HTTP/1.1 400 No URI.


HTTP/1.1 400 No URI





Don't use a Socket, use an HttpURLConnection.
– EJP
Jun 29 at 23:45


Socket


HttpURLConnection




1 Answer
1



Writing your own HTTP client from scratch is doable, but it's not simple. Check out this link: Java Tutorials: URLConnection. That is going to save you a lot of effort.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV