代码之家  ›  专栏  ›  技术社区  ›  Alex

将文件发送到API-C#

  •  3
  • Alex  · 技术社区  · 14 年前

    我正在尝试使用发送传真的API。

    下面是一个PHP示例: (不过我会用C)

    <?php
    //This is example code to send a FAX from the command line using the Simwood API
    //It is illustrative only and should not be used without the addition of error checking etc.
    
    $ch = curl_init("http://url-to-api-endpoint");
    $fax_variables=array(
    'user'=> 'test',
    'password'=> 'test',
    'sendat' => '2050-01-01 01:00',
    'priority'=> 10,
    'output'=> 'json',
    'to[0]' => '44123456789',
    'to[1]' => '44123456780',
    'file[0]'=>'@/tmp/myfirstfile.pdf',
    'file[1]' => '@/tmp/mysecondfile.pdf'
    ); 
    print_r($fax_variables);
    curl_setopt ($ch, CURLOPT_POST, 1);
    curl_setopt ($ch, CURLOPT_POSTFIELDS, $fax_variables); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $result=curl_exec ($ch);
    $info = curl_getinfo($ch);
    $result['http_code'];
    curl_close ($ch);
    print_r($result);
    
    ?>
    

    我的问题是-在C世界中,我将如何实现相同的结果?

    我需要建立一个post请求吗?

    理想情况下,我尝试使用rest-和构造一个URL,并使用httpwebrequest(get)来调用API。

    3 回复  |  直到 14 年前
        1
  •  1
  •   ChrisLively    14 年前

    无论何时发送数据,都应该使用日志。这与所涉及的技术无关。所有标准的HTTP方法(post、get、put、delete)都支持rest的思想。

    this wiki entry .


    更新: 更多信息

    有很多不同的选择。如您所描述的,一种方法是只使用httpwebrequest对象来构造请求并发送它。下面是使用该方法发布数据的示例( link 另一个是 here .

    另一种方法是使用wcf。微软对此有一些文档 here . 奥雷利有本书在上面 here .

        2
  •  0
  •   Community CDub    7 年前

    我通过调用 Curl utility 使用所需的命令行参数。见 this SO answer 以获取更多指导。curl是专门为这种需求而构建的,它是经过尝试的,并且是真实的,可以从命令行和批处理文件中使用它来测试URL和post字段。让curl做它最擅长的事情,并为您的业务逻辑留下C代码。

        3
  •  0
  •   code4life    14 年前

    基于发布的 specs ,httpwebrequest对象是使用c_与Simwood API通信时应该使用的对象。