# HTTPS原理

## HTTPS 範例

```javascript
const https = require('https')

const data = JSON.stringify({
  todo: 'Buy the milk'
})

const options = {
  hostname: 'test.com',
  port: 443,
  path: '/todos',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Content-Length': data.length
  }
}

const req = https.request(options, (res) => {
  console.log(`statusCode: ${res.statusCode}`)

  res.on('data', (d) => {
    process.stdout.write(d)
  })
})

req.on('error', (error) => {
  console.error(error)
})

req.write(data)
req.end()
```

## HTTPS 原理

### 流程可參考

<https://en.wikipedia.org/wiki/HTTPS>

### 如何驗證 server 證書

<https://www.zhihu.com/question/37370216/answer/74060132>

### windows中可以如下查看在認證列表內的機構證書

> <http://www.thewindowsclub.com/manage-trusted-root-certificates-windows>
>
> <https://www.youtube.com/watch?v=03DEqScfXlc>

![](https://2356031413-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0u4DAqfidGmTt67qrG%2F-M0u4EWtMiZmhb1fV2nz%2F-M0u4_l8mp5Qs_tz5CWv%2F79.png?generation=1582596295073024\&alt=media)

### TLS handshake

<https://www.cloudflare.com/zh-tw/learning/ssl/what-happens-in-a-tls-handshake/>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://easonwang.gitbook.io/web_advance/https/httpsyuan-li.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
