Is it possible to use the Net library to perform an http POST request?

polaris · · 369 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>[Solved] I&#39;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&#39;t receive anything. What am I doing wrong?</p> <pre><code>conn, err := net.Dial(&#34;tcp&#34;, &#34;127.0.0.1:80&#34; ) fmt.Fprintf(conn, &#34;POST /handleupload.php HTTP/1.0\r\n\r\n&#34;) n, err := conn.Write([]byte(&#34;ABCDEFGHIJ&#34;)) status, err := bufio.NewReader(conn).ReadString(&#39;z&#39;) fmt.Println( status ) </code></pre> <hr/>**评论:**<br/><br/>pib: <pre><p>You need to include a Content-Length header, otherwise the server doesn&#39;t know when it&#39;s done reading the request.</p> <p>Any reason you aren&#39;t using the built-in HTTP client in Go? It&#39;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 &#34;MUST&#34;, &#34;SHOULD&#34;, &#34;MAY&#34;, etc. in the context of RFCs.</p></pre>itsmontoya: <pre><p>This isn&#39;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

369 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传