Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# HttpWebRequest with XML Structured Data

Tags:

c#

I'm developing the client-side of a third party webservice. The purpose is that I send xml-file to the server. How should I attach the xml-file to the httpwebrequest? What contentType is needed? More suggestions?

I cannot use mtom or dime.ie because I am using httpwebrequest. I am unable to use WCF either.

like image 239
user690932 Avatar asked Apr 13 '11 18:04

user690932


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

Here is a very basic method of sending XML structured data using HttpWebRequest (by the way you need to use request.ContentType = "application/xml";) :

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(myUrl));
request.Method = "POST";
request.ContentType = "application/xml";
request.Accept = "application/xml";

XElement redmineRequestXML =
    new XElement("issue",
    new XElement("project_id", 17)
);

byte[] bytes = Encoding.UTF8.GetBytes(redmineRequestXML.ToString());

request.ContentLength = bytes.Length;

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

// Log the response from Redmine RESTful service
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
    Logger.Info("Response from Redmine Issue Tracker: " + reader.ReadToEnd());
}

I use this at one of my projects (NBug) to submit an issue report to my Redmine issue tracker which accepts XML structured data over web requests (via POST). If you need further examples, you can get a couple of fully featured examples here: http://nbug.codeplex.com/SourceControl/list/changesets (click 'Browse' under 'Latest Verion' label on the right then navigate to "NBug\Submit\Tracker\Redmine.cs")

like image 51
Teoman Soygul Avatar answered Oct 16 '22 16:10

Teoman Soygul