在.net中发送 xml post请求和接受xml 的post请求时,经常会遇到“远程服务器返回错误: (500) 内部服务器错误”。这里有2种解决办法:
第一种:修改请求端Content-Type 为“text/xml”(httpclient.Headers.Add("Content-Type", "text/xml");)
代码如下:
+ expand sourceview plaincopy to clipboardprint?
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
namespace Common
{
/// <summary>
/// 继承WebClient类
/// 提供向 URI 标识的资源发送数据和从 URI 标识的资源接收数据的公共方法
/// 支持以 http:、https:、ftp:、和 file: 方案标识符开头的 URI
/// </summary>
public class HttpClient : WebClient
{
#region 远程POST数据并返回数据
/// <summary>
/// 利用WebClient 远程POST数据并返回数据
/// </summary>
/// <param name="strUrl">远程URL地址</param>
/// <param name="strParams">参数</param>
/// <param name="RespEncode">POST数据的编码</param>
/// <param name="ReqEncode">获取数据的编码</param>
/// <returns></returns>
public static string PostData(string strUrl, string strParams, Encoding RespEncode, Encoding ReqEncode)
{
HttpClient httpclient = new HttpClient();
try
{
//打开页面
httpclient.Credentials = CredentialCache.DefaultCredentials;
//从指定的URI下载资源
byte[] responseData = httpclient.DownloadData(strUrl);
string srcString = RespEncode.GetString(responseData);
httpclient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
string postString = strParams;
// 将字符串转换成字节数组
byte[] postData = Encoding.ASCII.GetBytes(postString);
// 上传数据,返回页面的字节数组
responseData = httpclient.UploadData(strUrl, "POST", postData);
srcString = ReqEncode.GetString(responseData);
return srcString;
}
catch(Exception ex)
{
//记录异常日志
//释放资源
httpclient.Dispose();
return string.Empty;
}
}
#endregion
/// <summary>
/// 利用WebClient 远程POST XML数据并返回数据
/// </summary>
/// <param name="strUrl">远程URL地址</param>
/// <param name="strParams">参数</param>
/// <param name="RespEncode">POST数据的编码</param>
/// <param name="ReqEncode">获取数据的编码</param>
/// <returns></returns>
public static string PostXmlData(string strUrl, string strParams, Encoding RespEncode, Encoding ReqEncode)
{
HttpClient httpclient = new HttpClient();
try
{
//打开页面
httpclient.Credentials = CredentialCache.DefaultCredentials;
//从指定的URI下载资源
byte[] responseData = httpclient.DownloadData(strUrl);
string srcString = RespEncode.GetString(responseData);
httpclient.Headers.Add("Content-Type", "text/xml");
string postString = strParams;
// 将字符串转换成字节数组
byte[] postData = Encoding.ASCII.GetBytes(postString);
// 上传数据,返回页面的字节数组
responseData = httpclient.UploadData(strUrl, "POST", postData);
srcString = ReqEncode.GetString(responseData);
return srcString;
}
catch(Exception ex)
{
//记录异常日志
//释放资源
httpclient.Dispose();
return string.Empty;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
namespace Common
{
/// <summary>
/// 继承WebClient类
/// 提供向 URI 标识的资源发送数据和从 URI 标识的资源接收数据的公共方法
/// 支持以 http:、https:、ftp:、和 file: 方案标识符开头的 URI
/// </summary>
public class HttpClient : WebClient
{
#region 远程POST数据并返回数据
/// <summary>
/// 利用WebClient 远程POST数据并返回数据
/// </summary>
/// <param name="strUrl">远程URL地址</param>
/// <param name="strParams">参数</param>
/// <param name="RespEncode">POST数据的编码</param>
/// <param name="ReqEncode">获取数据的编码</param>
/// <returns></returns>
public static string PostData(string strUrl, string strParams, Encoding RespEncode, Encoding ReqEncode)
{
HttpClient httpclient = new HttpClient();
try
{
//打开页面
httpclient.Credentials = CredentialCache.DefaultCredentials;
//从指定的URI下载资源
byte[] responseData = httpclient.DownloadData(strUrl);
string srcString = RespEncode.GetString(responseData);
httpclient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
string postString = strParams;
// 将字符串转换成字节数组
byte[] postData = Encoding.ASCII.GetBytes(postString);
// 上传数据,返回页面的字节数组
responseData = httpclient.UploadData(strUrl, "POST", postData);
srcString = ReqEncode.GetString(responseData);
return srcString;
}
catch(Exception ex)
{
//记录异常日志
//释放资源
httpclient.Dispose();
return string.Empty;
}
}
#endregion
/// <summary>
/// 利用WebClient 远程POST XML数据并返回数据
/// </summary>
/// <param name="strUrl">远程URL地址</param>
/// <param name="strParams">参数</param>
/// <param name="RespEncode">POST数据的编码</param>
/// <param name="ReqEncode">获取数据的编码</param>
/// <returns></returns>
public static string PostXmlData(string strUrl, string strParams, Encoding RespEncode, Encoding ReqEncode)
{
HttpClient httpclient = new HttpClient();
try
{
//打开页面
httpclient.Credentials = CredentialCache.DefaultCredentials;
//从指定的URI下载资源
byte[] responseData = httpclient.DownloadData(strUrl);
string srcString = RespEncode.GetString(responseData);
httpclient.Headers.Add("Content-Type", "text/xml");
string postString = strParams;
// 将字符串转换成字节数组
byte[] postData = Encoding.ASCII.GetBytes(postString);
// 上传数据,返回页面的字节数组
responseData = httpclient.UploadData(strUrl, "POST", postData);
srcString = ReqEncode.GetString(responseData);
return srcString;
}
catch(Exception ex)
{
//记录异常日志
//释放资源
httpclient.Dispose();
return string.Empty;
}
}
}
}
调用:
string strUrl = http://localhost/testxml/Default.aspx;
string strParams = @"<?xml version=""1.0"" encoding=""UTF-8"" ?><channel><title>blogweather</title></channel>";
string returnValue = HttpClient.PostXmlData(strUrl, strParams, Encoding.UTF8, Encoding.UTF8);
这种方法适用于接收端是不同语言开发的web服务(jsp,php,asp.net等)
第二种:在我们平常开发异步接口的时候,往往需要开发一个windows service用来发送post请求(可以调用第一种方法);另外需要提供一个web接收端接收对方的异步调用。当接收的post数据是xml时,对方调用你的提供的接口是就会出现“远程服务器返回错误: (500) 内部服务器错误。”。
这是请求验证所导致的,只需要禁用掉请求验证就可以正常通讯了。在<@Page..%>中设置 ValidateRequest="false" 即可。(这里无需修改Content-type)
请求端调用:
string strUrl = http://localhost/testxml/Default2.aspx;
string strParams = @"<?xml version=""1.0"" encoding=""UTF-8"" ?><channel><title>blogweather</title></channel>";
string returnValue = HttpClient.PostData(strUrl, strParams, Encoding.UTF8, Encoding.UTF8);
或者
string returnValue = HttpClient.PostXmlData(strUrl, strParams, Encoding.UTF8, Encoding.UTF8);
接收post xml 请求:
string request;
using (Stream MyStream = Request.InputStream)
{
byte[] _tmpData = new byte[MyStream.Length];
MyStream.Read(_tmpData, 0, _tmpData.Length);
request = Encoding.UTF8.GetString(_tmpData);
}
Response.Write(request);