【发布时间】:2013-12-04 05:11:32
【问题描述】:
我在使用 WebClient 使用 htaccess 向 apache 服务器发送 POST 请求时遇到问题(我尝试使用 HttpRequest 和 WebRequest,但结果相同)。 我从 PHP 供应商那里得到了以下示例:
$soap = curl_init(url);
curl_setopt($soap, CURLOPT_POST, 1);
curl_setopt($soap, CURLOPT_RETURNTRANSFER, 1);
$XML = file_get_contents("test.xml");
$request = <<<XML
$XML
XML;
curl_setopt($soap, CURLOPT_HTTPHEADER, array('Content-Type: text/xml; charset=utf-8', 'Content-Length: '.strlen($request)));
curl_setopt($soap, CURLOPT_POSTFIELDS, $request);
$response = curl_exec($soap);
curl_close($soap);
我在 C# 上的版本是:
public static string Post(string login, string password, string url, string content) {
var result = String.Empty;
var uri = new Uri(url);
using (var client = new WebClient()) {
client.Credentials = new NetworkCredential(login, password);
client.Encoding = Encoding.UTF8;
client.Headers[HttpRequestHeader.ContentType] = "text/xml; charset=UTF-8";
client.Headers[HttpRequestHeader.Accept] = "application/xml";
result = client.UploadString(uri, content);
}
return result;
}
当我运行程序时,我遇到了 400 错误(错误请求)的异常。我已经使用 Fiddler2 嗅探请求并发现以下错误消息:“请求标头字段缺少':'分隔符” 谁能帮我找出请求中的问题以及服务器拒绝请求的原因?
P.S:请求头
POST http://production.is.topdelivery.ru/tests/xmlGate/index.php HTTP/1.1
Content-Type: text/xml; charset=utf-8
Authorization: Basic YmFiYWR1OmJhYmFkdXBhc3M=
Host: production.is.topdelivery.ru
Content-Length: 1617
Expect: 100-continue
【问题讨论】:
-
你解决过这个问题吗?我有同样的错误。