使用.net将xml通过HTTPS发布到Paypal Reporting API时出错

时间:2014-10-02 20:21:03

标签: .net xml paypal xmlhttprequest

您好我正在尝试使用.Net通过HTTPS将以下xml发布到PayPal报告引擎。我正在尝试使用.net以编程方式生成payPal报告。我查看了PayPal报告API的文档,并在xml文件下创建。我想将此XML发布到paypal以生成报告。请求的内容/类型应为text / plain。但我得到异常抛出消息我试图捕获异常,但无法做到这一点。如果我更改程序,我没有得到任何响应,根据PayPal文档,Response将是包含状态代码的xml文件。

DailyActivity.xml

<?xml version="1.0" encoding="UTF-8"?>
<reportingEngineRequest>
<authRequest>

<vendor>**</vendor>
<partner>**</partner>
<password>**</password>
</authRequest>
<runReportRequest>
<reportName>DailyActivityReport</reportName>
<reportParam>
<paramName>report_date</paramName>
<paramValue>2007-10-10</paramValue>
</reportParam>
<pageSize>50</pageSize>
</runReportRequest>
</reportingEngineRequest>

我的C#代码如下

  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
   using System.Net.Http;
  using System.Net.HttpWebRequest;

  namespace RunPaypalReport
  {
   class Program
   {
     static void Main(string[] args)
    {
        string URL = "https://payments-reports.paypal.com/test-reportingengine";
        string filename = "report.xml";
        try
        {
            WebRequest request = WebRequest.Create(URL);

            //Set values for the request back
            request.Method = "POST";
            request.ContentType = "text/plain";
            string postdata = GetTextFromXMLFile(filename);
            Console.Write(postdata);
            //   byte[] byteArray = Encoding.Default.GetBytes(postdata);
            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(postdata);
            request.ContentLength = bytes.Length;

            using (Stream os = request.GetRequestStream())
            {
                os.Write(bytes, 0, bytes.Length);
            }

            using (System.Net.WebResponse response = request.GetResponse())
            {
                if (response == null)
                    return;

                    using (System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream()))
                    {
                        string post = sr.ReadToEnd().Trim();

                        Console.Write(post);
                        sr.Close();
                        return;
                    }

            }
        }

        catch (Exception e)
        {
            Console.Write(e.Message);
        }
    }

    private static string GetTextFromXMLFile(string file)
    {
      StreamReader reader = new StreamReader(file);
      string ret = reader.ReadToEnd();
      reader.Close();
    // Console.Write(ret);
      return ret;
    }       }
  }

2 个答案:

答案 0 :(得分:2)

终于有了回应。错误发生在XML文件中。我们需要在请求xml中添加用户,用户应该是您的商家登录。通过添加用户我收到了xml响应。 下面是更新的XML

  <?xml version="1.0" encoding="utf-8" ?>
  <reportingEngineRequest>
  <authRequest>
  <user>Vendor/MerchantID</user>
  <vendor>Vendor/MerchantID</vendor>
  <partner>********</partner>
  <password>*******</password>
  </authRequest>
  <runReportRequest>
  <reportName>DailyActivityReport</reportName>
  <reportParam><paramName>report_date</paramName>
  <paramValue>2014-10-02</paramValue>
  </reportParam>
  <pageSize>50</pageSize>
  </runReportRequest>
  </reportingEngineRequest>

答案 1 :(得分:0)

需要使用PayPal报告端点URL创建request对象:

Live: https://payments-reports.paypal.com/reportingengine
Test: https://payments-reports.paypal.com/test-reportingengine

参考PayPal Reporting Guide

试试这个:

static void Main(string[] args)
{
    try
    {
        var liveEndpointUrl = "https://payments-reports.paypal.com/reportingengine";
        var testEndpointUrl = "https://payments-reports.paypal.com/test-reportingengine";

        var request = System.Net.WebRequest.Create(testEndpointUrl);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";

        UTF8Encoding encoder = new UTF8Encoding();
        byte[] data = encoder.GetBytes("report.xml");
        request.ContentLength = data.Length;
        var reqStream = request.GetRequestStream();
        reqStream.Write(data, 0, data.Length);
        reqStream.Close();
        var response = request.GetResponse();
        var reader = new System.IO.StreamReader(response.GetResponseStream());
        var str = reader.ReadToEnd();
        reader.Close();
        response.Close();
        Console.WriteLine(str);
    }
    catch (Exception ex)
    {
        Console.WriteLine("ERROR: " + ex.Message);
    }
}