Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6550179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:11:05+00:00 2026-05-25T12:11:05+00:00

I need to make a POST request with an xml data. String xml =

  • 0

I need to make a POST request with an xml data.

String xml = "";
byte[] data = System.Text.Encoding.UTF8.GetBytes(xml);
HttpClient.post(url, data, "text/xml")

Then I call the POST function:

public static String post(String url, byte[] data, String contentType){
        String body = "";
        body = getResponse("POST", url, data, contentType, 80);
        return body;
    }

Now I call this function to make the request / get the response:

public static String getResponse(String method, String url, byte[] data, String contentType, int serverPort)
    {
        String result = null;
        HttpWebRequest request = sendRequest(method, url, data, contentType, serverPort);
        HttpWebResponse response = null;
        try
        {
            response = (HttpWebResponse)request.GetResponse();
            if (response != null){
                // Get the stream associated with the response
                Stream receiveStream = response.GetResponseStream ();
                // Pipes the stream to a higher level stream reader
                StreamReader readStream = new StreamReader (receiveStream, System.Text.Encoding.UTF8);
                result = readStream.ReadToEnd ();
            }
        }
        catch(WebException ex)
        {
            if (ex.Status == WebExceptionStatus.ProtocolError){
                throw new HttpClientException("HTTP response error. ", (int)(((HttpWebResponse)ex.Response).StatusCode), ((HttpWebResponse)ex.Response).StatusDescription);
            }
            else{
                throw new HttpClientException("HTTP response error with status: " + ex.Status.ToString());
            }
        }
}

and

public static HttpWebRequest sendRequest(String method, String url, byte[] data, String contentType, int serverPort){
        HttpWebRequest request = null;
        try
        {
            UriBuilder requestUri = new UriBuilder(url);
            requestUri.Port = serverPort;
            request = (HttpWebRequest)WebRequest.Create(requestUri.Uri);
            request.Method = method;
            //
            if ((method == "POST") && (data != null) && (data.Length > 0)){
                request.ContentLength = data.Length;
                request.ContentType = ((String.IsNullOrEmpty(contentType))?"application/x-www-form-urlencoded":contentType);
                Stream dataStream = request.GetRequestStream ();
                dataStream.Write (data, 0, data.Length);
                // Close the Stream object.
                dataStream.Close ();
            }
        }
        catch(WebException ex)
        {
            if (ex.Status == WebExceptionStatus.ProtocolError){
                throw new HttpClientException("HTTP request error. ", (int)(((HttpWebResponse)ex.Response).StatusCode), ((HttpWebResponse)ex.Response).StatusDescription);
            }
            else{
                throw new HttpClientException("HTTP request error with status: " + ex.Status.ToString());
            }
        }
}

It always give me an HttpCliendException:

video_api.HttpClientException: HttpClient exception :HTTP response error.  with `code: 400` and `status: Bad Request`

But when I tried it with Firefox addon HTTP Resource Test, it ran fine and get 202 Accepted status with the same XML doc.

I consoled the content-type and data.length before the post request was called, the content-type was text/xml and the data.length is 143.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:11:05+00:00Added an answer on May 25, 2026 at 12:11 pm

    I have known some websites to be picky about request headers and return different results solely based on those values. Compare the request headers of the HTTP request in FireFox, and your request, and if you mimic the headers in the FireFox Resource Test, it will most likely work (Request.AddHeader("name", "value")). The other difference to note might be the user-agent which again can be picky for web servers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to make a HTTPS Post with some XML data, but I need
I need to make a POST request via cURL from the command line. Data
I need to make a request to an API, using REST (POST method) in
I need to make a jsonp POST request with the content type 'application/json'. I
Hey, I need to make a HTTP POST request with an array of NSDictionary
I need to make an HTTP POST request from outside the browser, but the
I need to make a nodejs client application that can send a POST request
I'm trying to make a POST request using HTTParty, in which I need the
I want to make a POST request to my Controller with jquery. function Send()
I need to make an AJAX request to some script from the onSave event

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.