1

I am trying to make a web API Post method call as follows but it not working as expected,xmlcontent seems OK but somehow the formatting seems messed up when the request is being sent and the response throws an error ,I double checked the XML from python and it works,is there a better way to create and send the XML?what am I doing wrong?

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

namespace WebApiXML
{
    public class Program
    {
        static void Main(string[] args)
        {
            testWCF2(); //Or whatever
            Console.ReadLine();
        }
        public static async Task testWCF2()
        {
            string xmlcontent = @"<SoftwareProductBuild>
  <BuildSource>DATASOURCE</BuildSource>
  <BuiltBy>username1</BuiltBy>
  <CreatedBy>username1</CreatedBy>
  <Name>username1_1959965_1969310_524f7fef-5b37-11e7-b4ee-f0921c133f10_UL.AB.1.2_test2</Name>
  <Status>Approved</Status>
  <BuiltOn>2017-06-27T06:20:30.275690</BuiltOn>
  <Tag>username1_1959965_1969310_524f7fef-5b37-11e7-b4ee-f0921c133f10_test2</Tag>
  <Keywords>
    <KeywordInfo>
      <Name>subystem</Name>
    </KeywordInfo>
  </Keywords>
  <SoftwareImageBuilds>
    <SoftwareImageBuild>
      <Type>LA</Type>
      <Name>username1_1959965_1969310_524f7fef-5b37-11e7-b4ee-f0921c133f10_UL.AB.1.2_test2</Name>
      <Location>\\location1\data1\PRECOMMIT_OS_DEF</Location>
      <Variant>PRECOMMIT_OS_DEF</Variant>
      <LoadType>Direct</LoadType>
      <Target>msm8998</Target>
      <SoftwareImages>
        <SoftwareImage>
          <Name>UL.AB.1.2</Name>
        </SoftwareImage>
      </SoftwareImages>
    </SoftwareImageBuild>
  </SoftwareImageBuilds>
</SoftwareProductBuild>";
            #region using
            using (var client = new System.Net.Http.HttpClient())
            {
                var response = await client.PostAsXmlAsync("http://server:8100/api/SoftwareProductBuild", xmlcontent);

                if (!response.IsSuccessStatusCode)
                {
                    //throw new InvalidUriException("Some error with details.");
                    Console.WriteLine(response);
                }
                Console.WriteLine("Printing DEV Pool Response\n");
            }
            #endregion
           //return null;
        }
    }

}

2
  • 1
    You're going to need to wait for your async call to finish. Also, you need to explain what the error is. "There was an error" could mean anything. Commented Jun 28, 2017 at 23:43
  • PostAsXmlAsync will try to serialize the object passed to it. So you have a string that contains XML and the try to post the string as XML. Use StringContent giving it the string value and set the content type to xml mime type. then post it. client.PostAsync(url, content) Commented Jun 29, 2017 at 0:26

1 Answer 1

1

PostAsXmlAsync will try to serialize the object passed to it. So you have a string that contains XML and then try to post the string as XML(Double serialization).

Use StringContent, giving it the XML string value and set the content type to appropriate media type, then post it. i.e. client.PostAsync(url, content)

using (var client = new System.Net.Http.HttpClient()) {
    var url = "http://server:8100/api/SoftwareProductBuild";
    var content = new StringContent(xmlcontent, Encoding.UTF8, "application/xml");
    var response = await client.PostAsync(url, content);
    if (response.IsSuccessStatusCode) {
        var responseBody = await response.Content.ReadAsStringAsync();
        Console.WriteLine("Printing DEV Pool Response\n");
        Console.WriteLine(responseBody);
    } else {
        Console.WriteLine(string.Format("Bad Response {0} \n", response.StatusCode.ToString()));
    }       
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.