zoukankan      html  css  js  c++  java
  • go_http

    httpSvr

    // HandleFunc registers the handler function for the given pattern
    // in the DefaultServeMux.
    // The documentation for ServeMux explains how patterns are matched.
    func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
    	DefaultServeMux.HandleFunc(pattern, handler)
    }
    

      

    // ListenAndServe listens on the TCP network address addr and then calls
    // Serve with handler to handle requests on incoming connections.
    // Accepted connections are configured to enable TCP keep-alives.
    //
    // The handler is typically nil, in which case the DefaultServeMux is used.
    //
    // ListenAndServe always returns a non-nil error.
    func ListenAndServe(addr string, handler Handler) error {
    	server := &Server{Addr: addr, Handler: handler}
    	return server.ListenAndServe()
    }
    

      

    package main
    
    import (
    	"fmt"
    	"net/http"
    )
    
    func Hello(w http.ResponseWriter, r *http.Request) {
    	fmt.Println("handle hello")
    	fmt.Fprintf(w, "dashboard page")
    }
    
    func Login(w http.ResponseWriter, r *http.Request) {
    	fmt.Println("handle login")
    	fmt.Fprintf(w, "login page")
    }
    
    
    func main() {
    	http.HandleFunc("/", Hello)
    	http.HandleFunc("/login/", Login)
    	err := http.ListenAndServe("127.0.0.1:8000", nil)
    	if err != nil {
    		fmt.Println("httpSvr listen failed")
    	}
    }
    

      

    httpClient

    package main
    
    import (
    	"fmt"
    	"io/ioutil"
    	"net/http"
    )
    
    func main() {
    	res,err := http.Get("http://127.0.0.1:8000/")
    	if err != nil {
    		fmt.Println("get err:",err)
    		return
    	}
    
    	data,err := ioutil.ReadAll(res.Body)
    	if err != nil {
    		fmt.Println("get data err:",err)
    		return
    	}
    	fmt.Println(string(data))
    }
    

      

  • 相关阅读:
    vs2005中 fstream 不支持中文路径问题的解决方法:
    CString char* string 互转
    OpenGl显示,任意大小的图片作为背景
    combo box
    Android 开发环境搭建
    打开子对话框 选择文件
    (转)Static MemoryLayout_shadow memory
    (转)深入剖析I/O约束
    (转)set_input_delay/ set_output_delay之图解
    (转)PrimeTime分析流程
  • 原文地址:https://www.cnblogs.com/jabbok/p/11330010.html
Copyright © 2011-2022 走看看