-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1786fd4
commit e2b9cb9
Showing
102 changed files
with
17,622 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ( | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
func main(){ | ||
reader := strings.NewReader("hello") | ||
req,_ := http.NewRequest("POST","http://www.abc.com",reader) | ||
client := http.Client{} | ||
client.Do(req) // 第一次会请求成功 | ||
client.Do(req) // 请求失败 | ||
} | ||
</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 ( | ||
"io" | ||
"net/http" | ||
"strings" | ||
"sync/atomic" | ||
) | ||
|
||
type Repeat struct{ | ||
reader io.ReaderAt | ||
offset int64 | ||
} | ||
|
||
// Read 重写读方法,使每次读取request.Body时能从指定位置读取 | ||
func (p *Repeat) Read(val []byte) (n int, err error) { | ||
n, err = p.reader.ReadAt(val, p.offset) | ||
atomic.AddInt64(&p.offset, int64(n)) | ||
return | ||
} | ||
|
||
// Reset 重置偏移量 | ||
func (p *Repeat) Reset(){ | ||
atomic.StoreInt64(&p.offset,0) | ||
} | ||
|
||
func (p *Repeat) Close() error { | ||
// 因为req.Body实现了readcloser接口,所以要实现close方法 | ||
// 但是repeat中的值有可能是只读的,所以这里只是尝试关闭一下。 | ||
if p.reader != nil { | ||
if rc, ok := p.reader.(io.Closer); ok { | ||
return rc.Close() | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func doPost() { | ||
client := http.Client{} | ||
reader := strings.NewReader("hello") | ||
req , _ := http.NewRequest("POST","http://www.abc.com",reader) | ||
req.Body = &Repeat{reader:reader,offset:0} | ||
client.Do(req) | ||
// 将偏移量重置为0 | ||
req.Body.(*Repeat).Reset() | ||
client.Do(req) | ||
} | ||
</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> |
Oops, something went wrong.