浏览器可以自动修改URL?

今天,一个同事访问http://www.ibm.com.cn网站时,觉得很奇怪,明明键入的是http://www.ibm.com.cn,怎么IE自动把它变成了http://www.ibm.com/cn/,而且已访问结果也是正确的。他觉得很费解,IE难道有这么智能?

 

只要了解HTTP协议,以上转换的原理很简单,其过程大致如下:

 

1.         为了从www.ibm.com.cn上取得网页,浏览器做了类似下面的事情。

telnet www.ibm.com.cn 80

GET / HTTP/1.1

Host: www.ibm.com.cn

Accept-Languge: zh_CN

 

这时WEB服务器上返回如下内容:

HTTP/1.1 301 Moved Permanently

Date: Thu, 16 Feb 2006 12:14:13 GMT

Server: IBM_HTTP_Server

Location: http://www.ibm.com/link/redirect.www.ibm.com/www.ibm.com.cn/

Content-Length: 268

Content-Type: text/html

 

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">

<html>

<head><title>301 Moved Permanently</title></head><body>

h1>Moved Permanently</h1>

<p>The document has moved

<a href="http://www.ibm.com/link/redirect.www.ibm.com/www.ibm.com.cn/">here</a>.

</p>

</body>

</html>

 

根据HTTP协议里的规定,3开头的错误码表示重定向,并告诉浏览器从指定的URL重新获取文件。

 

2.         于是,浏览器又做了类似下面的事情。

telnet www.ibm.com 80

GET  /link/redirect.www.ibm.com/www.ibm.com.cn/

Host: www.ibm.com.cn

Accept-Languge: zh_CN

 

这时WEB服务器上返回如下内容:

 

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">

<html><head><title>302 Found</title></head><body>

<h1>Found</h1>

<p>The document has moved

<a href="http://www.ibm.com/cn/">here</a>.</p>

</body></html>

 

还是重定向。

 

3.         于是,浏览器又做了类似下面的事情。

telnet www.ibm.com 80

GET /cn/ HTTP/1.1

Host: www.ibm.com.cn

Accept-Languge: zh_CN

 

这回终于得到了网面真正的内容。

 

参考:RFC 2616中的错误码:

 

Status-Code    =
            "100"  ; Section 10.1.1: Continue
          | "101"  ; Section 10.1.2: Switching Protocols
          | "200"  ; Section 10.2.1: OK
          | "201"  ; Section 10.2.2: Created
          | "202"  ; Section 10.2.3: Accepted
          | "203"  ; Section 10.2.4: Non-Authoritative Information
          | "204"  ; Section 10.2.5: No Content
          | "205"  ; Section 10.2.6: Reset Content
          | "206"  ; Section 10.2.7: Partial Content
          | "300"  ; Section 10.3.1: Multiple Choices
          | "301"  ; Section 10.3.2: Moved Permanently
          | "302"  ; Section 10.3.3: Found
          | "303"  ; Section 10.3.4: See Other
          | "304"  ; Section 10.3.5: Not Modified
          | "305"  ; Section 10.3.6: Use Proxy
          | "307"  ; Section 10.3.8: Temporary Redirect
          | "400"  ; Section 10.4.1: Bad Request
          | "401"  ; Section 10.4.2: Unauthorized
          | "402"  ; Section 10.4.3: Payment Required
          | "403"  ; Section 10.4.4: Forbidden
          | "404"  ; Section 10.4.5: Not Found
          | "405"  ; Section 10.4.6: Method Not Allowed
          | "406"  ; Section 10.4.7: Not Acceptable
 
    
    
          | "407"  ; Section 10.4.8: Proxy Authentication Required
          | "408"  ; Section 10.4.9: Request Time-out
          | "409"  ; Section 10.4.10: Conflict
          | "410"  ; Section 10.4.11: Gone
          | "411"  ; Section 10.4.12: Length Required
          | "412"  ; Section 10.4.13: Precondition Failed
          | "413"  ; Section 10.4.14: Request Entity Too Large
          | "414"  ; Section 10.4.15: Request-URI Too Large
          | "415"  ; Section 10.4.16: Unsupported Media Type
          | "416"  ; Section 10.4.17: Requested range not satisfiable
          | "417"  ; Section 10.4.18: Expectation Failed
          | "500"  ; Section 10.5.1: Internal Server Error
          | "501"  ; Section 10.5.2: Not Implemented
          | "502"  ; Section 10.5.3: Bad Gateway
          | "503"  ; Section 10.5.4: Service Unavailable
          | "504"  ; Section 10.5.5: Gateway Time-out
          | "505"  ; Section 10.5.6: HTTP Version not supported
          | extension-code
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过以下步骤实现: 1. 首先,你需要在 `config/filesystem.php` 文件中添加配置信息,以便连接到阿里云 OSS。 ``` 'disks' => [ // ... 'oss' => [ 'driver' => 'oss', 'access_id' => env('OSS_ACCESS_ID'), 'access_key' => env('OSS_ACCESS_KEY'), 'bucket' => env('OSS_BUCKET'), 'endpoint' => env('OSS_ENDPOINT'), 'isCName' => env('OSS_IS_CNAME', false), 'debug' => env('OSS_DEBUG', false), ], ], ``` 2. 然后,在你的控制器中,使用以下代码从 OSS 下载图片并执行自动下载: ```php use Illuminate\Support\Facades\Storage; public function downloadImage() { $filename = 'your_image_file_name.jpg'; // 替换成你的图片文件名 $path = 'your/oss/directory/' . $filename; // 替换成你的 OSS 目录和文件名 $headers = [ 'Content-Type' => 'image/jpeg', 'Content-Disposition' => 'attachment; filename="' . $filename . '"', ]; return response( Storage::disk('oss')->get($path), 200, $headers ); } ``` 3. 如果你需要修改文件名,可以在下载之前先将图片下载到本地,修改文件名后再返回给浏览器。示例代码如下: ```php use Illuminate\Support\Facades\Storage; public function downloadImage() { $filename = 'your_image_file_name.jpg'; // 替换成你的图片文件名 $path = 'your/oss/directory/' . $filename; // 替换成你的 OSS 目录和文件名 $tempFilename = 'temp_' . $filename; // 临时文件名 $tempPath = storage_path('app/public/temp/' . $tempFilename); // 临时文件路径 // 从 OSS 下载图片到本地 Storage::disk('oss')->download($path, $tempPath); // 修改文件名 $newFilename = 'new_image_file_name.jpg'; // 替换成你的新文件名 $newPath = storage_path('app/public/temp/' . $newFilename); // 替换成你的新文件路径 rename($tempPath, $newPath); // 返回给浏览器 $headers = [ 'Content-Type' => 'image/jpeg', 'Content-Disposition' => 'attachment; filename="' . $newFilename . '"', ]; return response( file_get_contents($newPath), 200, $headers ); } ``` 注意,上面的示例代码中使用了本地磁盘驱动器 `public`,你需要在 `config/filesystem.php` 文件中配置该磁盘驱动器: ``` 'disks' => [ // ... 'public' => [ 'driver' => 'local', 'root' => storage_path('app/public'), 'url' => env('APP_URL') . '/storage', 'visibility' => 'public', ], ], ``` 另外,你需要在 `storage/app/public` 目录下创建一个 `temp` 目录,用于存放临时文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值