首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >文件到C# cUrl中

文件到C# cUrl中
EN

Stack Overflow用户
提问于 2015-10-06 12:56:29
回答 1查看 529关注 0票数 0

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

不知怎么,我解决不了这个问题,我用了谷歌,但没有帮上忙。我使用libcurl.NET绑定来解决这个问题,但对我来说没有机会。

现在我想问你能不能帮我解决我的问题。我需要将此代码转换为:

代码语言:javascript
复制
<?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);
}

}

?>

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

现在我得到了这个C#代码

代码语言:javascript
复制
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;
    }

}

}

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

代码语言:javascript
复制
$method = self::METHOD_GET

也不知道我是不是把这部分改对了。

代码语言:javascript
复制
if ($headers) {



if ($postParams) {

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

代码语言:javascript
复制
$usableHeaders[] = sprintf('%s: %s', $name, $value);
Error: "Use of unassigned local variable 'usableHeaders'

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

我在这里找不到和这部分差不多的东西

代码语言:javascript
复制
http_build_query($urlParams);

希望你们能帮我一把。

顺便请原谅我的英语。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-06 13:44:30

我试着一个一个地回答:

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

self::METHOD_GET指向类内部的const String method_get = "GET";,这样您就可以像this.method_get一样编写它。

也不知道我是不是把这部分改对了。

差不多吧。像这样一条线

代码语言:javascript
复制
if ($headers) {

在C#中是这样的:

代码语言:javascript
复制
if (header != null && header.lenght > 0) { 

这是不完整的,检查它是否是一个数组!$postParam参数也是如此。

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

PHP还是C#?

对于checkForError函数,我不知道$curl变量是什么。

与依赖注入一样,您可以通过按值调用函数,将配置好的变量和所有的curl设置一起分配给函数。

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

很棘手,我也知道,也许您可以在Microsoft中搜索,首先搜索卷曲,然后.我找到了一个链接,也许有帮助:http://uniapple.net/blog/?p=1992

在这里,我没有找到与这部分http_build_query($urlParams)相等的东西;

请参阅How to build a query string for a URL in C#?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32970593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档