C#HttpWebRequest的使用XML结构化数据 [英] C# HttpWebRequest with XML Structured Data

查看:233
本文介绍了C#HttpWebRequest的使用XML结构化数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个第三方Web服务的客户端。的目的是,我发送XML的文件到服务器。
我应该如何附上XML的文件到HttpWebRequest的?的contentType什么需要?更多的建议?

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?

我不能因为我使用HttpWebRequest的使用MTOM或dime.ie。我无法要么使用WCF。

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

推荐答案

下面是方式发送使用HttpWebRequest的(XML结构化数据的一个非常基本的方法你需要使用request.ContentType =应用程序/ XML):

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());
}



我用这个在我的项目之一(的NBug )提交到我的管理平台问题跟踪问题报告,在web请求(通过POST)接受XML结构化数据。如果您需要进一步的例子,你可以得到一对夫妇在这里功能齐全的例子: http://nbug.codeplex.com/SourceControl /列表/变更(点击浏览下的最新优化版本标签右边然后导航到NBug\Submit\Tracker\Redmine.cs)

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")

这篇关于C#HttpWebRequest的使用XML结构化数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆