如何使用套接字在控制台上打印状态代码

huangapple go评论59阅读模式
英文:

How to print the status code on the console using socket

问题

我正在尝试获取响应代码,我找到了一种通过这段代码获取状态代码的方法:Ch2 = reader.readLine();

这段代码Ch2 = reader.readLine();会让您读取第一行,从而获取状态代码。

例如,响应可能会是这样的:HTTP 200 OK

但问题是,当我发送其他内容时,例如这个内容:"GET /Qatar/ HTTP/1.1\r\n\r\n"

响应将会是第一次发送的第二行,类似于这样:expires: Sat, 01 Jan 2000 00:00:00 GMT

有人能帮我解决这个问题吗?

这是我的代码:

sops.write(my_UTF8Byte);
				
String Ch2 = "";
Ch2 = reader.readLine(); 
System.out.println(Ch2); // 第一行是 HTTP 200 OK
					
// 第二个内容

sops.flush();
sops.write(my_UTF8Byte);
sops.flush();
Ch2 = reader.readLine();
System.out.println(Ch2); // 不会打印 HTTP 200 OK,会打印...

(代码部分未翻译)

英文:

I'm trying to get the response code and I found a way to get the status code via this code Ch2 = reader.readLine();

This code Ch2 = reader.readLine(); Will let you read the first line and you will get the status code

And the response for example will be like this HTTP 200 OK

But the problem is when I send other content for example this content "GET /Qatar/ HTTP/1.1\r\n\r\n"

The response will be the second line of the first sending like this expires: Sat, 01 Jan 2000 00:00:00 GMT

Can anyone help me to solve this problem?

Here is my code

	sops.write(my_UTF8Byte);
			
	String Ch2 = "";
    Ch2 = reader.readLine(); 
    System.out.println(Ch2); // The first line is HTTP 200 OK
				
			// The Second content


	sops.flush();
	sops.write(my_UTF8Byte);
	sops.flush();
	Ch2 = reader.readLine();
	System.out.println(Ch2); // It will not print HTTP 200 OK It will print 

答案1

得分: 3

如果你打算使用套接字与HTTP服务器进行通信,你必须彻底阅读HTTP规范。你需要理解这个协议,并编写符合协议的客户端代码。否则,它将无法工作。

你正在犯的关键错误:

  • 你需要消耗服务器发送的所有报头行,然后才能读取响应的主体部分。你只读取了状态行...然后就把其他部分留下了。因此,当你尝试从第二个“GET”请求读取响应时,实际上你读取的是第一个请求的响应中的报头行。

  • 你似乎试图在一个套接字连接上发送两个请求,一个接一个地发送。这是可能的,但是有很多事情你需要做才能使它工作。请参考规范中的持久连接部分。

但实际上,我建议你不要这样做。不要使用裸套接字与HTTP服务器通信。这需要太多的工作,而且很容易出现微小(或严重!)的错误。

而是应该使用一个HTTP客户端库!

英文:

If you are going to try to talk to an HTTP server using a socket, you must read the HTTP specifications ... thoroughly. You need to understand the protocol and write your client-side code to conform to the protocol. Otherwise it won't work.

Key mistakes you are making:

  • You need to consume all of the header lines that are sent by the server, and then the body of the response. You are reading just the status line ... and leaving the rest of them. So when you try to read the response from the second "GET" request, what you are actually reading is header lines from the response to the first request.

  • You seem to be trying to send two requests over one socket connection, one after the other. This is possible, but there are a bunch of things that you need to do to make it work. Refer to the Persistent Connections section of the spec.

But actually, my recommendation is that you don't do this. Don't use a bare socket to talk to a HTTP server. It is too much work, and too easy to get it subtly (or grossly!) wrong.

Use an HTTP client library instead!

huangapple
  • 本文由 发表于 2020年8月26日 16:02:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63593180.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定