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
.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.