Go语言中的HTTP client, server非常简单。具体如下。

HTTP Server

package  main

import (
"fmt"
"html"
"io/ioutil"
"log"
"net/http" ) func main() { http.HandleFunc("/bar", func (w http.ResponseWriter, r *http.Request){
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path)) if r.Method == "POST" {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Println("Read failed:", err)
}
defer r.Body.Close() log.Println("b:", string(b))
} else { log.Println("ONly support Post")
w.Write([]byte("Only support post"))
} }) log.Fatal(http.ListenAndServe(":8080", nil)) }

HTTP Client

  1. GET方式

    package main
    
    import (
    "io/ioutil"
    "log"
    "net/http" ) func main() { url := "http://127.0.0.1:8080/bar" resp, err := http.Get(url)
    if err != nil {
    log.Println("Get failed:", err)
    return
    } defer resp.Body.Close() if resp.StatusCode != http.StatusOK {
    log.Println("statuscode:", resp.StatusCode) } content, err := ioutil.ReadAll(resp.Body)
    if err != nil {
    log.Println("Read failed:", err)
    } log.Println("content:", string(content)) }
  2. POST方式

    package main
    
    import (
    "bytes"
    "io/ioutil"
    "log"
    "net/http" ) func main() { url := "http://127.0.0.1:8080/bar"
    contentType := "application/json;charset=utf-8" b := []byte("Hello, Server")
    body := bytes.NewBuffer(b) resp, err := http.Post(url, contentType, body)
    if err != nil {
    log.Println("Post failed:", err)
    return
    } defer resp.Body.Close() content, err := ioutil.ReadAll(resp.Body)
    if err != nil {
    log.Println("Read failed:", err)
    return
    } log.Println("content:", string(content)) }

注意:

response的Body使用后记得close,即:

resp.Body.Close()

关于如何传递json格式数据,可以参考博文go http 传递json数据

go http client, http server的更多相关文章

  1. UDP编程中client和server中使用recvfrom和sendto的区别

    client中:      sendto(sfd,buf,strlen(buf),0,(struct sockaddr *)&saddr,len);      recvfrom(sfd,buf ...

  2. tmux protocol version mismatch (client 7, server 6)

    $ tmux attach protocol version mismatch (client 7, server 6) $ pgrep tmux 3429 $ /proc/3429/exe atta ...

  3. New full duplex HTTP tunnel implementation (client and server)

    https://issues.jboss.org/browse/NETTY-246?page=com.atlassian.jirafisheyeplugin:fisheye-issuepanel —— ...

  4. jvm的client和server

    最近研究c++代码调用java的jar,发现64位的下的jvm在server路径,而32位的jvm则存在client路径下面,于是十分好奇,查了下,这里做个记录 JVM Server模式与client ...

  5. Android简单的聊天室开发(client与server沟通)

    请尊重他人的劳动成果.转载请注明出处:Android开发之简单的聊天室(client与server进行通信) 1. 预备知识:Tcp/IP协议与Socket TCP/IP 是Transmission ...

  6. The client and server cannot communicate, because they do not possess a common algorithm

    The client and server cannot communicate, because they do not possess a common algorithm This was re ...

  7. onvif协议client与server对接

    happytimesoft有完整的c语言开发的onvif client和server,一共1000$,真便宜,haha. http://www.happytimesoft.com/products/m ...

  8. 带入gRPC:gRPC Streaming, Client and Server

    带入gRPC:gRPC Streaming, Client and Server 原文地址:带入gRPC:gRPC Streaming, Client and Server 前言 本章节将介绍 gRP ...

  9. ESP8266 station模式下建立client、server TCP连接

    程序实现内容: 1.在station模式下,ESP8266作为client.server进行TCP连接2.实现数据的发送.接收(同时回传)实现思路:TCP网络通信分层为:应用层.网络层.数据链路层.物 ...

随机推荐

  1. Qt经典—线程、事件与Qobject

    介绍 You’re doing it wrong. — Bradley T. Hughes 线程是qt channel里最流行的讨论话题之一.许多人加入了讨论并询问如何解决他们在运行跨线程编程时所遇到 ...

  2. 快速切题 poj1129 Channel Allocation

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12334   Accepted: 63 ...

  3. bzoj1648

    题解: 简单灌水 然后统计一下 代码: #include<bits/stdc++.h> using namespace std; ; int ne[N],num,fi[N],n,k,m,x ...

  4. bzoj1091

    题解: 暴力枚举顺序 然后计算几何 代码: #include<bits/stdc++.h> ],lp=; double v1,v2,ans=1e10; struct pos { doubl ...

  5. Solr查询参数

    引自:http://www.cnblogs.com/zhangweizhong/p/5056884.html 一.基本查询 q  查询的关键字,此参数最为重要,例如,q=id:1,默认为q=*:*, ...

  6. Crystal Report Error: Either the Crystal Reports registy key permission are insufficient or the Crystal Reports runtime is not installed correctly

    在64位 Windows 7中水晶报表的错误: Crystal Report Error: Either the Crystal Reports registy key permission are ...

  7. JSONField解决序列化与反序列化字段匹配问题

    需求:调用第三方数据,数据格式为Json,并提供一个接口将获取的第三方数据给本公司其他部门调用. 处理流程:第三方Json--反序列化实体--保存到本地数据库--查询数据--序列化Json数据供本公司 ...

  8. 在 windows 开发 reactNative 的环境 搭建过程 react-native-android

    安装的东西挺多的, 从 jdk 到c++环境 到node , python, 各种模拟器 http://bbs.reactnative.cn/topic/10/%E5%9C%A8windows%E4% ...

  9. 由pg_xlogdump统计信息想到的问题

    最近深入理解了Checkpoint的相关逻辑,再来看WAL日志的一些设置,又有了新的收获. 1.回顾pg_xlogdump出来的wal日志信息: 2.wal中FPI的占比很高问题分析: 3.重申ful ...

  10. Android输入法框架系统(上)

    输入法,就是用来输入字符(包括英文,俄文,中文)的工具.输入法你可以看成是一种字符发生器,它将输入数据触摸事件或者按键事件转化为其他更丰富的字符.在PC时代,输入法的原始输入来自实体键盘,鼠标,然后输 ...