2017-08-10 40 views
-1

我需要將xml文件發佈到我的供應商的API。 他給了我一個鏈接,accesskey,secretkey,並告訴我推送xml文件。 他也給了我一個示例代碼來發布json中的XML文件。請在下面找到它。在C#中發佈一個xml文件到API .net

{ 
    "id": "22307a79-89dc-914e-5cf1-098f992e5054", 
    "name": "Sample Request", 
    "description": "", 
    "order": [ 
     "36f699c6-0765-3e31-fdf8-863a3c3d508c" 
    ], 
    "folders": [], 
    "timestamp": 1497005641974, 
    "owner": "1189835", 
    "public": false, 
    "requests": [ 
     { 
      "id": "36f699c6-0765-3e31-fdf8-863a3c3d508c", 
      "headers": "accessKey: 1057550\nsecretKey: t3stH11p7550\n", 
      "headerData": [ 
       { 
        "key": "accessKey", 
        "value": "1057550", 
        "description": "", 
        "enabled": true 
       }, 
       { 
        "key": "secretKey", 
        "value": "dummy7550", 
        "description": "", 
        "enabled": true 
       } 
      ], 
      "url": "https://feeds.vendor.com/API.php/Job/postHttpJob", 
      "queryParams": [], 
      "preRequestScript": null, 
      "pathVariables": {}, 
      "pathVariableData": [], 
      "method": "POST", 
      "data": [ 
       { 
        "key": "content", 
        "value": "<?xml version=\"1.0\"?>\n<JobPositionPostings>\n <JobPositionPosting>\n  <JobAction>ADD</JobAction>\n  <JobType>p</JobType>\n  <JobPositionPostingID>27</JobPositionPostingID>\n <SummaryText>Desired Candidate Profile Summary Text - Required Competencies/Skills/Leadership E-mail\n   </SummaryText>\n   <JobExperience>\n   <MinimumExperience>2</MinimumExperience>\n   <MaximumExperience>4</MaximumExperience>\n   </JobExperience>\n   <JobQualifications>\n   <UGQualifications />\n   <UGSpecializations />\n   <PGQualifications />\n   <PGSpecializations />\n   </JobQualifications>\n  </JobPositionRequirements>\n  </JobPositionInformation>\n  <HowToApply>\n  <ApplicationMethods>\n   <ByWeb>\n   <URL>\n   </URL>\n   <ApplyIntegration>1</ApplyIntegration>\n   </ByWeb>\n  </ApplicationMethods>\n  </HowToApply>\n  </JobPositionPosting>\n</JobPositionPostings>\n", 
        "type": "text", 
        "enabled": true 
       }, 
       { 
        "key": "", 
        "value": "", 
        "description": "", 
        "type": "text", 
        "enabled": true 
       } 
      ], 
      "dataMode": "params", 
      "version": 2, 
      "tests": null, 
      "currentHelper": "normal", 
      "helperAttributes": {}, 
      "time": 1501909208689, 
      "name": "Sample Request", 
      "description": "Sample Request with sample XML", 
      "collectionId": "22307a79-89dc-914e-5cf1-098f992e5054", 
      "responses": [] 
     } 
    ] 
} 

我在C#.NET的工作,我如何將它轉換爲C#.NET或有人可以讓我知道我如何使用C#.NET推動XML文件到我的供應商的API。

在此先感謝。

+1

然而你的供應商告訴你這樣做。檢查他們的API文檔或聯繫他們的支持 – musefan

回答

0

下面的代碼是等價的C#代碼,謝謝大家的幫助和支持。在下面的代碼中的fp1是一個文件上載控件。

var client = new RestClient("https://your-api-linkhere"); 
var request = new RestRequest("/api-method-here",Method.POST); 
request.AddHeader("cache-control", "no-cache"); 
request.AddHeader("content-type", "application/x-www-form-urlencoded"); 
request.AddHeader("secretkey", "skey"); 
request.AddHeader("accesskey", "akey"); 
Encoding en = Encoding.GetEncoding("iso-8859-1"); 
XmlDocument doc = new XmlDocument(); 
string textLine = ""; 
StreamReader reader = new StreamReader(fp1.FileContent); 
do 
{ 
    textLine = textLine + reader.ReadLine(); 
} while (reader.Peek() != -1); 
reader.Close(); 
string requestText = string.Format("content={0}", HttpUtility.UrlEncode(textLine, en)); 
request.AddParameter("application/x-www-form-urlencoded", requestText, ParameterType.RequestBody); 
IRestResponse response = client.Execute(request); 
0

我知道有些人不喜歡解決方案。但是你可以使用的String.format()方法創建Ĵ

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq; 

namespace ConsoleApplication72 
{ 
    class Program 
    { 
     const string FILENAME = @"c:\temp\test.xml"; 
     static void Main(string[] args) 
     { 
      XDocument doc = XDocument.Load(FILENAME); 
      string xml = doc.ToString(); 

      string json = string.Format(
       "{" + 
        "\"id\": \"22307a79-89dc-914e-5cf1-098f992e5054\"," + 
        "\"name\": \"Sample Request\"," + 
        "\"description\": \"\"," + 
        "\"order\": [" + 
         "\"36f699c6-0765-3e31-fdf8-863a3c3d508c\"" + 
        "]," + 
        "\"folders\": []," + 
        "\"timestamp\": 1497005641974," + 
        "\"owner\": \"1189835\"," + 
        "\"public\": false," + 
        "\"requests\": [" + 
         "{" + 
          "\"id\": \"36f699c6-0765-3e31-fdf8-863a3c3d508c\"," + 
          "\"headers\": \"accessKey: 1057550\nsecretKey: t3stH11p7550\n\"," + 
          "\"headerData\": [" + 
           "{" + 
            "\"key\": \"accessKey\"," + 
            "\"value\": \"1057550\"," + 
            "\"description\": \"\"," + 
            "\"enabled\": true" + 
           "}," + 
           "{" + 
            "\"key\": \"secretKey\"," + 
            "\"value\": \"dummy7550\"," + 
            "\"description\": \"\"," + 
            "\"enabled\": true" + 
           "}" + 
          "]," + 
          "\"url\": \"https://feeds.vendor.com/API.php/Job/postHttpJob\"," + 
          "\"queryParams\": []," + 
          "\"preRequestScript\": null," + 
          "\"pathVariables\": {}," + 
          "\"pathVariableData\": []," + 
          "\"method\": \"POST\"," + 
          "\"data\": [" + 
           "{" + 
            "\"key\": \"content\"," + 
            "\"value\": \"{0}\"," + 
            "\"type\": \"text\"," + 
            "\"enabled\": true" + 
           "}," + 
           "{" + 
            "\"key\": \"\"," + 
            "\"value\": \"\"," + 
            "\"description\": \"\"," + 
            "\"type\": \"text\"," + 
            "\"enabled\": true" + 
           "}" + 
          "]," + 
          "\"dataMode\": \"params\"," + 
          "\"version\": 2," + 
          "\"tests\": null," + 
          "\"currentHelper\": \"normal\"," + 
          "\"helperAttributes\": {}," + 
          "\"time\": 1501909208689," + 
          "\"name\": \"Sample Request\"," + 
          "\"description\": \"Sample Request with sample XML\"," + 
          "\"collectionId\": \"22307a79-89dc-914e-5cf1-098f992e5054\"," + 
          "\"responses\": []" + 
         "}" + 
        "]" + 
       "}", 
       xml); 

     } 
    } 

} 

兒子像下面