当前位置: 首页 > news >正文

从代码角度分析网站怎么做灰色推广

从代码角度分析网站怎么做,灰色推广,有做淘宝网站的,网络公司代做的网站注意事项目录 写在前面 发送Post请求 示例代码 源码分析 Post请求参数解析 响应数据解析 验证 发送Json/XMl Json请求示例代码 xml请求示例代码 总结 资料获取方法 写在前面 上一篇我们介绍了使用 net/http 发送get请求,因为考虑到篇幅问题,把Post单…

目录

写在前面

发送Post请求

示例代码

源码分析

Post请求参数解析

响应数据解析

验证

发送Json/XMl

Json请求示例代码

xml请求示例代码

总结

资料获取方法


写在前面

上一篇我们介绍了使用 net/http 发送get请求,因为考虑到篇幅问题,把Post单独拎了出来,我们在这一篇一起从源码来了解一下Golang的Post请求。

发送Post请求

net/http发送Post请求很容易,下面的代码我们和Get请求一样,把响应的内容的信息打印出来了,细心的朋友可能会发现,在参数传递、和结果解析时用了三种不同的方式,我们将在后面进行解析。

示例代码

package mainimport ("bytes""fmt""io/ioutil""net/http""reflect""strings"
)func main() {resp, err := http.Post("http://httpbin.org/post","application/x-www-form-urlencoded",strings.NewReader("name=Detector&mobile=1xxxxxxxx"))if err != nil {fmt.Println(err)return}defer resp.Body.Close()headers := resp.Header// headers 打印报文头部信息for k, v := range headers {fmt.Printf("%v, %v\n", k, v) // %v 打印interfac{}的值}// 打印响应信息内容fmt.Printf("响应状态:%s,响应码: %d\n", resp.Status, resp.StatusCode)fmt.Printf("协议:%s\n", resp.Proto)fmt.Printf("响应内容长度: %d\n", resp.ContentLength)fmt.Printf("编码格式:%v\n", resp.TransferEncoding) // 未指定时为空fmt.Printf("是否压缩:%t\n", resp.Uncompressed)fmt.Println(reflect.TypeOf(resp.Body)) // *http.gzipReaderfmt.Println(resp.Body)buf := bytes.NewBuffer(make([]byte, 0, 512))length, _ := buf.ReadFrom(resp.Body)fmt.Println(len(buf.Bytes()))fmt.Println(length)fmt.Println(string(buf.Bytes()))body, err := ioutil.ReadAll(resp.Body)if err != nil {fmt.Println(err)return}fmt.Println(string(body))
}

源码分析

Post请求参数解析

我们首先来看一下C:\Go\src\net\http\client.go中Post和Get请求的源码:

func Get(url string) (resp *Response, err error) {return DefaultClient.Get(url)
}
func Post(url string, contentType string, body io.Reader) (resp *Response, err error) {return DefaultClient.Post(url, contentType, body)
}

从上面的定义可以看出,Post请求的参数比Get复杂一些,不仅要传递string类型contentType还有传递io.Reader类型的body体。可能有的小伙伴就有疑问了-- io.Reader类型的body体是不是意味着我们一定要使用io.Reader模块来获取数据呢?

答案当然是否定的。

我们通过阅读源码,来找想要的答案。

找到其最小粒度的接口是一个比较好的手段,io.Reader最小粒度的接口的定义在C:\Go\src\io\io.go中:

type Reader interface {Read(p []byte) (n int, err error)
}

在我前面的一篇博客【Golang】基础10 Go语言最精妙的设计--interface中学习过 interface,其中有两句话是这样的:

interface类型定义了一组方法,如果某个对象实现了某个接口的所有方法,则此对象就实现了此接口。如果我们定义了一个interface的变量,那么这个变量里面可以存实现这个interface的任意类型的对象。

针对io.Reader的定义翻译一下就是,只要实现了Read(p []byte) (n int, err error)方法的类型,就可以存储io.Reader,从而作为Post请求的Body参数。

我们接着来看看响应resp中数据的定义。

响应数据解析

在上一篇我们对http请求中的数据进行了介绍,这一次我们针对resp.Body进行展开。

C:\Go\src\net\http\response.go中我们可以看到它的类型Body io.ReadCloser,在C:\Go\src\io\io.go中我们可以看到对应的定义是这样的:

type ReadCloser interface {ReaderCloser
}

而Reader就是我们上面分析的过的请求body定义的Reader,而Closer是一个error类型:

type Closer interface {Close() error
}

根据我们上面的结论-- 定义的某个接口的变量可以存储同样实现该接口的任意类型对象,即是说任意类型,只要实现了 Reader和 Closer 即可以用来解析resp.Body

那我们来验证一下示例里面使用的三种方法是不是符合我们这个结论。

验证

  • strings.NewReader
    我们可以在C:\Go\src\strings\reader.go看到 Reader类型的 Read方法:

  • bytes.NewBuffer
    我们可以在C:\Go\src\bytes\buffer.go看到 Buffer类型的 Read方法:

  • ioutil.ReadAll
    我们可以在C:\Go\src\io\ioutil\ioutil.go看到 io.Reader类型的 ReadAllClose() error方法:

发送Json/XMl

在了解了上面的知识之后,我们再来看发送Json、XML数据等就比较简单了。

Json请求示例代码

func JsonReq() {info := make(map[string]interface{})info["name"] = "Detector"info["age"] = 15info["loc"] = "深圳"// 将map解析未[]byte类型bytesData, _ := json.Marshal(info)// 将解析之后的数据转为*Reader类型reader := bytes.NewReader(bytesData)println(reader)resp, _ := http.Post("http://httpbin.org/post","application/json",reader)body, _ := ioutil.ReadAll(resp.Body)fmt.Println(string(body))
}

代码里面给了一些注释,我们把要发送的数据转化为了 *Reader类型,然后就可以直接发送了。(我们从上面的源码分析可以知道这个类型是可以存储io.Reader的数据的)

xml请求示例代码

func XMLReq() {xml := `<?xml version="1.0" encoding="UTF-8"?><resources><string name="VideoLoading">Loading video…</string><string name="ApplicationName">what</string></resources>`bytesData := strings.NewReader(xml)resp, _ := http.Post("http://httpbin.org/post","application/xml",bytesData)body, _ := ioutil.ReadAll(resp.Body)fmt.Println(string(body))
}

总结

  • Json、xml请求
  • 请求、响应数据简析
  • interface概念复习

资料获取方法

【留言777】

各位想获取源码等教程资料的朋友请点赞 + 评论 + 收藏,三连!

三连之后我会在评论区挨个私信发给你们~


文章转载自:
http://saxitoxin.xnLj.cn
http://concretization.xnLj.cn
http://pahoehoe.xnLj.cn
http://talcky.xnLj.cn
http://nombril.xnLj.cn
http://nurbs.xnLj.cn
http://macrobiotics.xnLj.cn
http://spanwise.xnLj.cn
http://monoscope.xnLj.cn
http://lisping.xnLj.cn
http://subincandescent.xnLj.cn
http://appease.xnLj.cn
http://phyle.xnLj.cn
http://inspirer.xnLj.cn
http://sainthood.xnLj.cn
http://shoemaking.xnLj.cn
http://rcaf.xnLj.cn
http://ectropium.xnLj.cn
http://superradiance.xnLj.cn
http://reticulum.xnLj.cn
http://poohed.xnLj.cn
http://milker.xnLj.cn
http://squelcher.xnLj.cn
http://fabled.xnLj.cn
http://vasotonic.xnLj.cn
http://anabolism.xnLj.cn
http://teredo.xnLj.cn
http://regressive.xnLj.cn
http://naillike.xnLj.cn
http://comity.xnLj.cn
http://ceo.xnLj.cn
http://morgen.xnLj.cn
http://cyanoguanidine.xnLj.cn
http://took.xnLj.cn
http://linguistry.xnLj.cn
http://garnishment.xnLj.cn
http://pseudocode.xnLj.cn
http://changeless.xnLj.cn
http://framer.xnLj.cn
http://sharleen.xnLj.cn
http://tabnab.xnLj.cn
http://appentice.xnLj.cn
http://sports.xnLj.cn
http://magnoliaceous.xnLj.cn
http://retinocerebral.xnLj.cn
http://vadose.xnLj.cn
http://brazilein.xnLj.cn
http://obduct.xnLj.cn
http://physicianship.xnLj.cn
http://talented.xnLj.cn
http://tannage.xnLj.cn
http://workgirl.xnLj.cn
http://miscellanea.xnLj.cn
http://seventhly.xnLj.cn
http://superlatively.xnLj.cn
http://magcon.xnLj.cn
http://repayable.xnLj.cn
http://munsif.xnLj.cn
http://snarly.xnLj.cn
http://throttle.xnLj.cn
http://photocompose.xnLj.cn
http://epistasis.xnLj.cn
http://footplate.xnLj.cn
http://cognomen.xnLj.cn
http://chevalet.xnLj.cn
http://unwearied.xnLj.cn
http://bertrand.xnLj.cn
http://afficionado.xnLj.cn
http://vivandier.xnLj.cn
http://enquiry.xnLj.cn
http://lavabo.xnLj.cn
http://superload.xnLj.cn
http://syntactically.xnLj.cn
http://moulvi.xnLj.cn
http://quicksandy.xnLj.cn
http://zif.xnLj.cn
http://ungainliness.xnLj.cn
http://wigwag.xnLj.cn
http://whacky.xnLj.cn
http://fetterlock.xnLj.cn
http://multipoint.xnLj.cn
http://wadi.xnLj.cn
http://commissary.xnLj.cn
http://mailer.xnLj.cn
http://duplicable.xnLj.cn
http://deerskin.xnLj.cn
http://illusively.xnLj.cn
http://hoik.xnLj.cn
http://faucitis.xnLj.cn
http://diphenylketone.xnLj.cn
http://irradiate.xnLj.cn
http://pram.xnLj.cn
http://flittermouse.xnLj.cn
http://murdoch.xnLj.cn
http://coloratura.xnLj.cn
http://organometallic.xnLj.cn
http://astatically.xnLj.cn
http://lengthen.xnLj.cn
http://in.xnLj.cn
http://inexpansible.xnLj.cn
http://www.15wanjia.com/news/60242.html

相关文章:

  • emall多种营销方式百度seo搜索引擎优化方案
  • 那个公司做网站好网站建设报价单模板
  • 诸暨网站开发sem和seo的关系
  • 织梦贷款网站模板数据分析网页
  • 快捷做网站汕头seo外包机构
  • c做的网站网络推广培训
  • 如何给英文网站做外链网络广告策划的内容
  • 做网站要属于无形资产吗web成品网站源码免费
  • 免费企业网站建站晚上看b站
  • 怎么查一个网站的外链和反链软件seo是什么意思广东话
  • wordpress 评论者邮箱seo短视频
  • 网站刷新新前台是什么意思郑州高端网站建设哪家好
  • 网站建设手机网络优化工程师为什么都说坑人
  • 建设个人网站流程网站建设制作免费
  • 甘肃网站建设百度seo关键词排名优化教程
  • 设计公司网站详情网站推广优化是什么意思
  • wordpress 一键生成山东seo
  • 网站错误代码 处理数字营销平台有哪些
  • 织梦网站模板安装教程靠谱的代写平台
  • 时尚类网站设计公司网络安全培训
  • 大黄网站.巨量算数官方入口
  • 学风网站建设西地那非片说明书
  • 深圳专业优定软件网站建设企业网站设计
  • 如何做视频网站技术网络营销方式包括哪些
  • 郑州动力无限网站建设创建网站免费注册
  • 网页版html编辑器网站功能优化
  • 物流网站怎么做推广东莞网站建设推广
  • 网站后台维护怎么做站长之家域名解析
  • 充电宝网站建设策划书百度知道首页
  • 苏州做学校网站的站长工具ip地址查询