使用nodejs爬取某网站数据

记2021-07-20日推荐算法岗面试经历

  返回  

go: net/http实践

2021/7/21 0:05:16 浏览:

1. server端代码

package main

import (
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"os"
)

const Filepath = "C:\\Documents\\CODE\\GO\\myHttpServerFile"

func getFilesList(w http.ResponseWriter, req *http.Request) {
	var buffer bytes.Buffer
	buffer.WriteString("The Request method is ")
	buffer.WriteString(req.Method)
	buffer.WriteString(" \nThe FilesList are: ")
	filesList, _ := ioutil.ReadDir(Filepath)
	for i := range filesList {
		buffer.WriteString("\n")
		buffer.WriteString(filesList[i].Name())
	}
	w.Header().Set("Content-Type", "text/plain")
	_, err := w.Write(buffer.Bytes())
	if err != nil {
		fmt.Println("写入错误")
		return
	}
}

func getFile1(w http.ResponseWriter, req *http.Request) {
	//file1Path := File + "\\file_1.txt"
	f, _ := os.Open(Filepath + "\\file_1.txt")
	defer f.Close()
	context, _ := ioutil.ReadAll(f)
	_, _ = io.WriteString(w, string(context))
}

func postFile1(w http.ResponseWriter, req *http.Request) {
	body, _ := ioutil.ReadAll(req.Body)
	fmt.Println(string(body))
	err := os.WriteFile(Filepath+"\\file_1.txt", body, 0777)
	if err == nil {
		_, _ = fmt.Fprintln(w, "post success")
	} else {
		log.Fatal("post write error")
	}
}

func main() {
	http.HandleFunc("/myHttpServerFile", getFilesList)
	http.HandleFunc("/myHttpServerFile/file_1", getFile1)
	http.HandleFunc("/myHttpServerFile/post_file_1", postFile1)
	err := http.ListenAndServe(":12345", nil)
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}

2.client端代码

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
)

func main() {
	c := &http.Client{}
	URL := "/myHttpServerFile"
	getMain(c, URL)
	URL = "/myHttpServerFile/file_1"
	getMain(c, URL)
	URL = "/myHttpServerFile/post_file_1"
	postMain(c, URL)
	URL = "/myHttpServerFile/file_1"
	getMain(c, URL)
}

func getMain(c *http.Client, URL string) {
	r, _ := c.Get("http://localhost:12345"+URL)
	defer r.Body.Close()
	body, _ := ioutil.ReadAll(r.Body)
	fmt.Println(string(body))
}

func headMain(c *http.Client, URL string) {
	r, _ := c.Head("http://localhost:12345"+URL)
	defer r.Body.Close()
	for k, v := range r.Header {
		fmt.Printf("key: %s   value: %s \n", k, v)
	}
}

func postMain(c *http.Client, URL string) {
	postContext := strings.NewReader("testPostMain")
	req, _ := http.NewRequest("POST", "http://localhost:12345"+URL, postContext)
	req.Header.Set("Content-Type", "application/json;charset=UTF-8")
	req.Header.Set("myCookie", "cookieTest")
	r, _ := c.Do(req)
	defer r.Body.Close()
	body, _ := ioutil.ReadAll(r.Body)
	fmt.Println(string(body))
}

3.运行结果

C:\Documents\CODE\GO\myHttpServerFile路径下文件为:
在这里插入图片描述先运行server端代码,再运行client端代码。
运行结果如下:
在这里插入图片描述

联系我们

如果您对我们的服务有兴趣,请及时和我们联系!

服务热线:18288888888
座机:18288888888
传真:
邮箱:888888@qq.com
地址:郑州市文化路红专路93号