通过HTTPS而不是HTTP连接 [英] Connect through HTTPS instead of HTTP

查看:172
本文介绍了通过HTTPS而不是HTTP连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用一个简单的API,我想以安全的方式做到这一点。
它目前正在使用套接字和端口80.据我所知,端口80是打开的,它似乎不是一个安全的连接。

I want to use a simple API and i want to do it in the secure way. It is currently using sockets and port 80. As far as I know port 80 is open and it doesn't seem such a secure connection.

作为要发送的数据包含用户和密码我想使用HTTPS而不是HTTP来保证安全。

As the data to send contains user and password i want to use HTTPS instead of HTTP to make it secure.

我想知道它是否只是改变这一行这么简单;

I was wondering if it is so simple as just changing this line;

$headers = "POST /api/api.php HTTP/1.0\r\n";

另一行

$headers = "POST /api/api.php HTTPS/1.0\r\n";

并将端口更改为 443

这是连接函数:

// api connect function
function api_connect($Username, $Password, $ParameterArray)
{
   // Create the URL to send the message.
   // The variables are set using the input from an HTML form

   $err = array();
   $url = "api.text-connect.co.uk";
   $headers = "POST /api/api.php HTTP/1.0\r\n";
   $headers .= "Host: ".$url."\r\n";

   // Create post string
   // Username and Password
   $poststring = "Username=".$Username."&";
   $poststring .= "Password=".$Password;

   // Turn the parameter array into the variables

   while (list($Key, $Value)=@each($ParameterArray))
   {
      $poststring .= "&".$Key."=".urlencode($Value);
   }

   // Finish off the headers
   $headers .= "Content-Length: ".strlen($poststring)."\r\n";
   $headers .= "Content-Type: application/x-www-form-urlencoded\r\n";


   // Open a socket
   $http = fsockopen ($url, 80, $err[0], $err[1]);
   if (!$http)
   {
      echo "Connection to ".$url.":80 failed: ".$err[0]." (".$err[1].")";
      exit();
   }

   // Socket was open successfully, post the data.
   fwrite ($http, $headers."\r\n".$poststring."\r\n");

   // Read the results from the post
   $result = "";
   while (!feof($http))
   {
      $result .= fread($http, 8192);
   }

   // Close the connection
   fclose ($http);

   // Strip the headers from the result
   list($resultheaders, $resultcode)=split("\r\n\r\n", $result, 2);

   return $resultcode;
}
?>


推荐答案


我想知道是否它很简单

I was wondering if it is so simple as

不,它不是。它真的,实际上不是。

No, it isn't. It really, really isn't.

HTTPS通过SSL进行HTTP隧道传输。因此,您根本不需要更改HTTP请求的内容。

HTTPS is HTTP tunnelled over SSL. So you don't change the content of the HTTP request at all.

您需要在执行HTTP操作之前执行所有SSL握手。

You do need to perform all the SSL handshaking before you do the HTTP stuff though.

SSL是加密的,因此很难。不要尝试重新发明这个轮子。使用 cURL 等库。

SSL is crypto, it is therefore hard. Don't try reinventing this wheel. Use a library such as cURL.

这篇关于通过HTTPS而不是HTTP连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆