<p>[Solved] I'm trying to do a simple RAW data UPLOAD using the net library (not the net/http). I have a simple php script that just spits out whatever was sent to it. The problem is that the php script doesn't receive anything. What am I doing wrong?</p>
<pre><code>conn, err := net.Dial("tcp", "127.0.0.1:80" )
fmt.Fprintf(conn, "POST /handleupload.php HTTP/1.0\r\n\r\n")
n, err := conn.Write([]byte("ABCDEFGHIJ"))
status, err := bufio.NewReader(conn).ReadString('z')
fmt.Println( status )
</code></pre>
<hr/>**评论:**<br/><br/>pib: <pre><p>You need to include a Content-Length header, otherwise the server doesn't know when it's done reading the request.</p>
<p>Any reason you aren't using the built-in HTTP client in Go? It's a lot of work to properly implement the HTTP protocol when you could just use an existing standard library instead.</p>
<p>Edit: I should also add, if you are implementing HTTP from scratch for educational purposes, you should definitely get comfortable reading RFCs, especially <a href="https://www.w3.org/Protocols/HTTP/1.0/spec.html" rel="nofollow">https://www.w3.org/Protocols/HTTP/1.0/spec.html</a></p>
<p>Make sure you pay attention to the meanings of "MUST", "SHOULD", "MAY", etc. in the context of RFCs.</p></pre>itsmontoya: <pre><p>This isn't entirely true. The content length helps to determine the end, but a connection close works just fine.</p></pre>fubo: <pre><p>Yup. Half-closed is well-defined in TCP and is perfectly fine to use.</p></pre>lil_bobcat: <pre><p>Yes, this turned out to be the problem. Thanks!</p></pre>jbendotnet: <pre><p>^ this</p></pre>tusharsingh: <pre><p>What do the error or other logs for the server hosting the script show? It may require additional headers.</p></pre>lil_bobcat: <pre><p>Thanks for that, I was missing the content length header, once added it works like a charm.</p></pre>: <pre><p>[deleted]</p></pre>lil_bobcat: <pre><p>Turns out it was a missing Content Length header, adding it made the above snippet work. Thanks for the help.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传