php文件转换成c# cUrl

本文关键字:cUrl 转换 文件 php | 更新日期: 2023-09-27 18:16:10

我正在为学校做一个小项目。我需要将一个PHP - cUrl文件转换为C#

不知怎么的,我解决不了,我用谷歌,但没有帮助。我使用libcurl。. NET绑定来解决这个问题,但我没有机会。 现在我想问你是否可以帮助我解决我的问题。我需要转换以下代码:
<?php
class Curl {
const METHOD_GET = 'GET';
const METHOD_POST = 'POST';
const METHOD_PUT = 'PUT';
const METHOD_DELETE = 'DELETE';
const CURL_TIMEOUT_IN_SECS = 15;
public static $successFullHttpCodes = array(200, 201, 204);
public function call($url, $urlParams = array(), $postParams = null, $method = self::METHOD_GET, $headers = array()) {
    $finalUrl = $url . $this->getUrlParameterString($urlParams);
    $data = $this->makeCurlCall($finalUrl, $postParams, $method, $headers);
    return $data;
}
/**
 * Creates a curl call for the given url, automatically validates the return value for errors.
 * If an error has been found a new Exception will be thrown.
 * 
 * @param string $url
 * @param array $postParams Parameters for Post and Put-Requests
 * @param string $method HTTP-Method (GET, PUT, POST, DELETE)
 * @param string $headers HTTP-Headers
 * @throws Exception
 */
private function makeCurlCall($url, $postParams = array(), $method = self::METHOD_GET, $headers = array()) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_VERBOSE, 0);
    curl_setopt($curl, CURLOPT_TIMEOUT, self::CURL_TIMEOUT_IN_SECS);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
    if ($headers) {
        $usableHeaders = array();
        foreach ($headers as $name => $value) {
            $usableHeaders[] = sprintf('%s: %s', $name, $value);
        }
        curl_setopt($curl, CURLOPT_HTTPHEADER, $usableHeaders);
    }
    if ($postParams) {
        curl_setopt($curl, CURLOPT_POSTFIELDS, $postParams);
    }
    $data = curl_exec($curl);
    $this->checkForError($curl, $data);
    return $data;
}
/**
 * Checks for any errors in the api response.
 * If an error has been found a new Exception will be thrown.
 *
 * @param string $data the resulting data
 * @throws Exception
 */
private function checkForError($curl, $data) {
    $curlInfo = curl_getinfo($curl);
    if (isset($curlInfo['http_code']) && !in_array($curlInfo['http_code'], self::$successFullHttpCodes)) {
        throw new Exception($curlInfo['http_code'] ? $data : 'could not get a response from the service', $curlInfo['http_code'] ? $curlInfo['http_code'] : 500);
    }
}
/**
 * Builds a valid http query
 *
 * @param array $urlParams
 * @return string
 */
private function getUrlParameterString(array $urlParams) {
    if (!$urlParams) {
        return "";
    }
    return "?" . http_build_query($urlParams);
}
}
?>

需要将此文件转换为1to1才能在我的项目中使用。

现在我有这个c#代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SeasideResearch.LibCurlNet;
namespace ConsoleApplication1
{
class CurlCall
{
    const String method_get = "GET";
    const String method_post = "POST";
    const String method_put = "PUT";
    const String method_delete = "DELETE";
    const int curl_timeout_in_secs = 15;
    public static int[] successFullHttpCodes = { 200, 201, 204 };
    public String call(String url, String[] urlParams, String postParams, String method, String[] header)
    {
        String data = "";
        String finalUrl = "";
        finalUrl = url + this.getUrlParameterString(urlParams);
        return data;
    }
    private String makeCurlCall(String url, String[] postParams, String method, String[] header)
    {
        try
        {
            Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);
            Easy easy = new Easy();
            Easy.WriteFunction wf = OnWriteData;
            easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYPEER, 0);
            easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYHOST, 2);
            easy.SetOpt(CURLoption.CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
            easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);
            easy.SetOpt(CURLoption.CURLOPT_URL, "http://google.com/index.html");
            easy.SetOpt(CURLoption.CURLOPT_VERBOSE, 0);
            easy.SetOpt(CURLoption.CURLOPT_TIMEOUT, curl_timeout_in_secs);
            easy.SetOpt(CURLoption.CURLOPT_CUSTOMREQUEST, method);
            if(header != null)
            {
                String[] usableHeaders;
                for(int i = 0; i <= header.Length; i++)
                {
                    usableHeaders[i] = string.Format("%s: %s", header[i], i);
                }
                easy.SetOpt(CURLoption.CURLOPT_HTTPHEADER, usableHeaders);
            }
            if (postParams != null)
            {
                easy.SetOpt(CURLoption.CURLOPT_POSTFIELDS, postParams);
            }
            easy.Perform();
            easy.Cleanup();
            Curl.GlobalCleanup();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
            Console.ReadKey();
        }
        String data;
        data = "0";
        this.checkForError("", data);
        return data;
    }
    private void checkForError(String curl, String data)
    {
        String curlInfo;
        Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);
        Easy easy = new Easy();
        curlinfo = easy.GetInfo(CURLINFO, curl);
        if()
        {
        }
    }
    private String getUrlParameterString(String[] urlParams)
    {
        if(urlParams != null)
        {
            return "";
        }
        return "?";
    }
    public static Int32 OnWriteData(Byte[] buf, Int32 size, Int32 nmemb,
    Object extraData)
    {
        Console.Write(System.Text.Encoding.UTF8.GetString(buf));
        Console.ReadKey();
        return size * nmemb;
    }
}

}

我无法转换这部分,我不理解self::键。

$method = self::METHOD_GET

也不知道我是否把这些部分转换对了

if ($headers) {

if ($postParams) {

在这段代码中,我得到一个错误。

$usableHeaders[] = sprintf('%s: %s', $name, $value);
Error: "Use of unassigned local variable 'usableHeaders'

关于checkForError函数,我不明白$curl变量是什么…也不知道怎么用getinfo $curlInfo = curl_getinfo($curl);

这里我没有找到等于这部分的东西

http_build_query($urlParams);

希望你们能帮帮我。

php文件转换成c# cUrl

我试着一个一个回答:

我无法转换这部分,我不理解self::键。

self::METHOD_GET指向你的类内部const String method_get = "GET";,所以你可以像this.method_get那样写它。

也不知道我是否把这些部分转换对了

近。像这样的一行

if ($headers) {

在c#中是这样的:

if (header != null && header.lenght > 0) { 

这是不完整的,检查它是否是一个数组!与$postParam参数相同。

在这段代码中,我得到一个错误。

PHP还是c# ?

关于checkForError函数,我不明白$curl是什么变量。

就像依赖注入一样,你可以通过按值调用的方式给你的函数配置变量和所有的curl设置。

也不知道如何使用getinfo $curlInfo = curl_getinfo($curl);

很棘手,我也知道,也许你在微软MSDN中搜索,从搜索curl开始,然后…我找到了一个链接,也许它有帮助:http://uniapple.net/blog/?p=1992

这里我没有找到等于这部分的东西http_build_query ($ urlParams);

参见如何在c#中为URL构建查询字符串?