PHP / VB.NET POST请求

时间:2019-04-23 15:50:51

标签: php vb.net

我正在尝试连接到我们的电信提供商所提供的API,他们提供给我的只是一个要托管的PHP网页。

我反过来尝试将其转换为VB .NET,这对我来说意义更大,并且将要收集的数据更容易地插入SQL等中。

我正在努力让API甚至响应它具有的基本“ AUTH”。

<?php
// IPscape API auth details
define(IPS_API_KEY, "KEY");
define(IPS_API_USERPWD, "USER:PASSWORD");

// API URLs
define(IPS_AUTH_SERVICE, "URL");
define(IPS_CAMPAIGN_SERVICE, "URL");

define(VERBOSE, true);
define(DEBUG, false);

// IPscape API auth
if (VERBOSE || DEBUG) { echo "IPscape API auth starting\n"; }
$ips_auth_response = ips_api_auth();
if (VERBOSE || DEBUG) { echo "IPscape API auth complete\n"; if (DEBUG) { echo print_r($ips_auth_response."\n",true); }}

现在我的PHP非常粗略,但是我很确定auth正在调用以下函数:

function ips_api_auth() {

    $api_key = "apikey=".IPS_API_KEY;

    $ips_auth = curl_init(IPS_AUTH_SERVICE);
    curl_setopt($ips_auth, CURLOPT_POST, 1);
    curl_setopt($ips_auth, CURLOPT_POSTFIELDS, $api_key);
    curl_setopt($ips_auth, CURLOPT_USERPWD, IPS_API_USERPWD);
    curl_setopt($ips_auth, CURLOPT_RETURNTRANSFER, true);

    $ips_auth_response = curl_exec($ips_auth);

    curl_close($ips_auth);
    return $ips_auth_response;
}
?>

我基本上是这样输入的:

Public IPS_API_KEY As String = "KEY"
    Public IPS_API_USERPWD As String = "USER:PASSWORD"

    Public IPS_AUTH_SERVICE As String = "URL"
    Public IPS_CAMPAIGN_SERVICE As String = "URL"
    Sub Main()

        Dim authresponse As String

        authresponse = ips_api_auth()

    End Sub

    Public Function ips_api_auth As String

        ServicePointManager.Expect100Continue = true
        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12

        Dim myReq as HttpWebRequest
        Dim myResp as HttpWebResponse

        myReq = HttpWebRequest.Create(IPS_AUTH_SERVICE)

        myreq.Method = "POST"
        myReq.contenttype = "application/json"
        myReq.Headers.Add("Authorization","Basic " & Convert.ToBase64String(Encoding.UTF8.GetBytes(IPS_API_USERPWD)))


        Dim myData As String = "apikey=" & IPS_API_KEY
        myReq.GetRequestStream.Write(System.Text.Encoding.UTF8.GetBytes(myData), 0, System.Text.Encoding.UTF8.GetBytes(myData).Count)
        myResp = myReq.GetResponse
        Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream)
        Dim myText As String
        myText = myreader.ReadToEnd

    End Function

无论如何尝试,我都会得到

System.Net.WebException: 'The remote server returned an error: (400) Bad Request.'

现在,我很确定自己已经覆盖了所有内容,而且凝视的时间已经足够长,以至于我可以用另一双眼睛来做。

任何人对PHP的了解都比对我更了解。

欢呼

0 个答案:

没有答案