Skip to content

Commit

Permalink
Site updated: 2024-09-01 22:24:26
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Sep 1, 2024
1 parent 1786fd4 commit e2b9cb9
Show file tree
Hide file tree
Showing 102 changed files with 17,622 additions and 0 deletions.
294 changes: 294 additions & 0 deletions 2019/02/21/golang_http_request/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,294 @@



<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> [Golang] http.Request复用 | Domechn.</title>
<meta name="description" content="A minimalist theme for hexo.">
<!-- 标签页图标 -->

<link rel="shortcut icon" href="/favicon.png" type="image/x-icon">


<!-- 图标库 -->
<link href="https://cdn.jsdelivr.net/npm/remixicon@2.2.0/fonts/remixicon.css" rel="stylesheet">
<!-- 动画库 -->

<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/fushaolei/cdn-white@1.0/css/animate.css"/>

<!-- css文件 -->

<link rel="stylesheet" href="/css/white.css">

<!-- 代码高亮 -->



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10.1.1/styles/github.css">



<meta name="generator" content="Hexo 6.3.0"><link rel="alternate" href="/atom.xml" title="Not Bad" type="application/atom+xml">
</head>


<body>

<div class="menu-outer">
<div class="menu-inner">
<div class="menu-site-name animate__animated animate__fadeInUp">
<a href="/">
Domechn.
</a>

</div>
<div class="menu-group">
<ul class="menu-ul">

<a href="/" class="nav-link">
<li class="menu-li animate__animated animate__fadeInUp">
HOME
</li>
</a>

<a href="/archives" class="nav-link">
<li class="menu-li animate__animated animate__fadeInUp">
BLOG
</li>
</a>

<a href="/mypages/footprints" class="nav-link">
<li class="menu-li animate__animated animate__fadeInUp">
FOOTPRINTS
</li>
</a>

<a href="/atom.xml" class="nav-link">
<li class="menu-li animate__animated animate__fadeInUp">
RSS
</li>
</a>



<a href="/search">
<li class="menu-li animate__animated animate__fadeInUp">
<i class="ri-search-line"></i>
</li>
</a>

<li class="menu-li animate__animated animate__fadeInUp" id="mobile-menu">
<i class="ri-menu-line"></i>
</li>

</ul>

</div>

</div>
</div>
<div id="mobile-main" class="animate__animated animate__fadeIn">
<div class="mobile-menu-inner">
<div class="mobile-menu-site-name animate__animated animate__fadeInUp">
<a href="/">
Domechn.
</a>
</div>
<div class="mobile-menu-group" id="mobile-close">
<i class="ri-close-line"></i>
</div>

</div>

<div class="mobile-menu-div">

<a href="/" class="mobile-nav-link">
<div class="mobile-menu-child animate__animated animate__fadeInUp">
<span>HOME</span>
</div>
</a>

<a href="/archives" class="mobile-nav-link">
<div class="mobile-menu-child animate__animated animate__fadeInUp">
<span>BLOG</span>
</div>
</a>

<a href="/mypages/footprints" class="mobile-nav-link">
<div class="mobile-menu-child animate__animated animate__fadeInUp">
<span>FOOTPRINTS</span>
</div>
</a>

<a href="/atom.xml" class="mobile-nav-link">
<div class="mobile-menu-child animate__animated animate__fadeInUp">
<span>RSS</span>
</div>
</a>


<a href="/search">
<div class="mobile-menu-child animate__animated animate__fadeInUp">
<i class="ri-search-line"></i>
</div>
</a>

</div>


</div>

<div class="body-outer">
<div class="body-inner">

<article class="post-inner">
<div class="post-content-outer">
<div class="post-intro">
<div class="post-title animate__animated animate__fadeInUp">[Golang] http.Request复用</div>
<div class="meta-intro animate__animated animate__fadeInUp">Feb 21 2019</div>

</div>
<div class="post-content-inner">
<div class="post-content-inner-space">

</div>
<div class="post-content-main animate__animated animate__fadeInUp">
<!-- top型目录 -->

<h3 id="复用针对除了-Get-以外的请求"><a href="#复用针对除了-Get-以外的请求" class="headerlink" title="复用针对除了 Get 以外的请求"></a>复用针对除了 Get 以外的请求</h3><pre><code class="go">package main

import (
&quot;net/http&quot;
&quot;strings&quot;
)

func main()&#123;
reader := strings.NewReader(&quot;hello&quot;)
req,_ := http.NewRequest(&quot;POST&quot;,&quot;http://www.abc.com&quot;,reader)
client := http.Client&#123;&#125;
client.Do(req) // 第一次会请求成功
client.Do(req) // 请求失败
&#125;
</code></pre>
<p>第二次请求会出错</p>
<p><code>http: ContentLength=5 with Body length 0</code></p>
<p>原因是第一次请求后 req.Body 已经读取到结束位置,所以第二次请求时无法再读取 body,<br>解决方法:重新定义一个 ReadCloser 的实现类替换 req.Body</p>
<pre><code class="go">package reader

import (
&quot;io&quot;
&quot;net/http&quot;
&quot;strings&quot;
&quot;sync/atomic&quot;
)

type Repeat struct&#123;
reader io.ReaderAt
offset int64
&#125;

// Read 重写读方法,使每次读取request.Body时能从指定位置读取
func (p *Repeat) Read(val []byte) (n int, err error) &#123;
n, err = p.reader.ReadAt(val, p.offset)
atomic.AddInt64(&amp;p.offset, int64(n))
return
&#125;

// Reset 重置偏移量
func (p *Repeat) Reset()&#123;
atomic.StoreInt64(&amp;p.offset,0)
&#125;

func (p *Repeat) Close() error &#123;
// 因为req.Body实现了readcloser接口,所以要实现close方法
// 但是repeat中的值有可能是只读的,所以这里只是尝试关闭一下。
if p.reader != nil &#123;
if rc, ok := p.reader.(io.Closer); ok &#123;
return rc.Close()
&#125;
&#125;
return nil
&#125;

func doPost() &#123;
client := http.Client&#123;&#125;
reader := strings.NewReader(&quot;hello&quot;)
req , _ := http.NewRequest(&quot;POST&quot;,&quot;http://www.abc.com&quot;,reader)
req.Body = &amp;Repeat&#123;reader:reader,offset:0&#125;
client.Do(req)
// 将偏移量重置为0
req.Body.(*Repeat).Reset()
client.Do(req)
&#125;
</code></pre>
<p>这样就不会报错了,因为也重写了 Close()方法,所以同时也解决了 request 重用时,req.Body 自动关闭的问题。</p>

<!-- 分类文章 -->

</div>
<div class="post-content-inner-space">

<div class="space-toc-main animate__animated animate__fadeInUp">

</div>

</div>
</div>
<!-- 评论 -->

</div>
</article>
</div>
</div>



<!-- 如果是home模式的话,不在首页就显示footer,如果不是home模式的话 所有都显示footer -->

<div class="footer-outer animate__animated animate__fadeInUp">
<div class="footer-inner">
<div class="footer-text">
<p>Power by <a target="_blank" rel="noopener" href="http://hexo.io/">Hexo</a> Theme by <a target="_blank" rel="noopener" href="https://github.com/FuShaoLei/hexo-theme-white">White</a></p>

</div>
<div class="footer-contact">
<ul class="footer-ul">

<li class="footer-li">
<a href="https://github.com/domechn" target="_blank">
<i class="ri-github-line"></i>
</a>
</li>

<li class="footer-li">
<a href="mailto:domdoumc@gmail.com" target="_blank">
<i class="ri-mail-line"></i>
</a>
</li>

</ul>
</div>
</div>
</div>






<script src="/js/white.js"></script>





<script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@9.18.1/build/highlight.min.js"></script>

<script>hljs.initHighlightingOnLoad();</script>


</body>
</html>
Loading

0 comments on commit e2b9cb9

Please sign in to comment.