【问题标题】:How to send and receive answers to a web api using ASP.NET C#如何使用 ASP.NET C# 向 Web api 发送和接收答案
【发布时间】:2018-12-23 14:16:47
【问题描述】:

我必须在 XML 中调用一个使用 EPP(可扩展供应协议)的 Web API 并返回一个 XML 作为响应。我还需要使用证书来调用它,但收到的响应为空。 PHP 中只有一个示例代码有效。

eppClient.php

<?php 
require_once 'config.php';
require_once 'eppFunctions.php';

//Get XML variable via POST method 
$xmlStr = stripslashes(trim($_POST['xmlStr']));

$response = send_epp_request($USER_CERTIFICATE_FILE, $EPP_SERVER_URL, $xmlStr);

if(!$response === false){

    //Show Server Response as XML
    header("Content-type: application/xml");
    echo $response;

}
?>

config.php

<?php
//Server URL Address For Sending XML
$EPP_SERVER_URL = "api.site.com/submit";

//URL CERTIFICATE FILE
$USER_CERTIFICATE_FILE = "";
?>

eppFunctions.php

<?php 
function send_epp_request_form($USER_CERTIFICATE_FILE, $EPP_SERVER_URL, $xmlStr)
{
    $XML = array('xmlStr' => $xmlStr);

    // create a new cURL resource
    $ch = curl_init();

    //FALSE to stop cURL from verifying the peer's certificate
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    //The name of a file containing a PEM formatted certificate. 
    curl_setopt($ch, CURLOPT_SSLCERT, $USER_CERTIFICATE_FILE);
    //TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    //The contents of the "User-Agent: "
    curl_setopt($ch, CURLOPT_USERAGENT, "IRNIC_EPP_Client_Sample");
    //The URL to fetch.
    curl_setopt($ch, CURLOPT_URL,"https://$EPP_SERVER_URL");
    //TRUE to do a regular HTTP POST.
    curl_setopt($ch, CURLOPT_POST, 1);
    //The full data to post in a HTTP "POST" operation.
    curl_setopt($ch, CURLOPT_POSTFIELDS, $XML);

    // grab URL and pass it to the browser
    $response = curl_exec($ch);

    if($response === false) {
        // echo Errors
        echo 'Curl error: ' . curl_error($ch);
    }
    // close cURL resource, and free up system resources
    curl_close ($ch);

    return $response;

}

function send_epp_request($USER_CERTIFICATE_FILE, $EPP_SERVER_URL, $xmlStr)
{
    // create a new cURL resource
    $ch = curl_init();

    //FALSE to stop cURL from verifying the peer's certificate
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    //The name of a file containing a PEM formatted certificate. 
    curl_setopt($ch, CURLOPT_SSLCERT, $USER_CERTIFICATE_FILE);
    //TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    //The contents of the "User-Agent: "
    curl_setopt($ch, CURLOPT_USERAGENT, "IRNIC_EPP_Client_Sample");
    //TRUE to do a regular HTTP POST.This POST is the normal application/x-www-form-urlencoded kind, most commonly used by HTML forms.
    curl_setopt($ch, CURLOPT_POST, false);
    //The URL to fetch.
    curl_setopt($ch, CURLOPT_URL,"https://$EPP_SERVER_URL");
    //The full data to post in a HTTP "POST" operation.
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlStr);

    // grab URL and pass it to the browser
    $response = curl_exec($ch);

    if($response === false) {
        // echo Errors
        echo 'Curl error: ' . curl_error($ch);
    }
    // close cURL resource, and free up system resources
    curl_close ($ch);

    return $response;

}
?>

我尝试过的:

我尝试了几种方式,如WebClient、HttpWebRequest或HttpClient,但都没有结果。

protected string WebApiCall (string Data, string Url)
    {
        //Url in https

        byte[] BytesOfData = Encoding.ASCII.GetBytes(Data);

        string CertFile = Server.MapPath("~/crt/abc.p12"); 
        string CertKey = "abc";
        string ResponseData = string.Empty;

        //ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
        //ServicePointManager.ServerCertificateValidationCallback = (a, b, c, d) => true;
        ServicePointManager.Expect100Continue = true;
        X509Certificate2Collection SrvCertificate = new X509Certificate2Collection();
        SrvCertificate.Import(CertFile, CertKey, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet);

        //X509Certificate2 SrvCertificate = new X509Certificate2(CertFile, CertKey);

        HttpWebRequest HttpWebRequest = (HttpWebRequest)WebRequest.Create(Url);
        HttpWebRequest.ClientCertificates = SrvCertificate;
        //HttpWebRequest.ClientCertificates.Add(SrvCertificate); 
        HttpWebRequest.Method = "POST";
        HttpWebRequest.Accept = "application/xml";
        HttpWebRequest.ContentType = "application/xml";
        HttpWebRequest.ContentLength = BytesOfData.Length;


        Stream RequestStream = HttpWebRequest.GetRequestStream();
        RequestStream.Write(BytesOfData, 0, BytesOfData.Length);
        RequestStream.Close();

        HttpWebResponse HttpWebResponse = (HttpWebResponse)HttpWebRequest.GetResponse();

        if (HttpWebResponse.StatusCode == HttpStatusCode.OK)
        {
            Stream ResponseStream = HttpWebResponse.GetResponseStream();
            ResponseData += new StreamReader(ResponseStream).ReadToEnd();
            return ResponseData;
        }

        return null;
    }

【问题讨论】:

    标签: c# php asp.net-web-api httpwebrequest


    【解决方案1】:

    你检查过这个图书馆吗? https://github.com/CodeMakerInc/EppLib.NET 我还在https://mobtowers.com/2013/03/14/how-to-write-an-epp-client-in-c/上找到了一篇很好的分步文章

    【讨论】:

      【解决方案2】:

      试试这个。

      string data = "{}";
      WebRequest myReq = WebRequest.Create(url);
       //set webreuqest properties e.g
          //myReq.Method = "POST";
          //myReq.ContentLength =  lenght;
       UTF8Encoding enc = new UTF8Encoding();
       using (Stream ds = myReq.GetRequestStream())
       {
       ds.Write(enc.GetBytes(data), 0, data.Length);
       }
       using (WebResponse wr = myReq.GetResponse())
       {
       Stream receiveStream = wr.GetResponseStream();
       using (StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8))
      {
      
          string content = reader.ReadToEnd();
      
      }
      }
      

      【讨论】:

        猜你喜欢
        • 2014-05-10
        • 1970-01-01
        • 2017-09-26
        • 2014-11-02
        • 1970-01-01
        • 2015-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多