2

我发现这段代码按预期工作:

var url = "https://limal.info/efulfilment.php";
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
var alternativeAnswer = Encoding.UTF8.GetString(new WebClient().UploadValues(url, new NameValueCollection() { { "xml", "test" } }));

然而,下面的代码让我很头疼:

var url = "https://limal.info/efulfilment.php";
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

var request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
request.Timeout = 5000; // I added this for you, so you only need to wait 5 sec...

using (var requestStream = request.GetRequestStream())
{
    var writer = new StreamWriter(requestStream);
    writer.Write("xml=test");
}
using (var response = request.GetResponse())
{
    using (var responseStream = response.GetResponseStream())
    {
        var reader = new StreamReader(responseStream);
        var answer = reader.ReadToEnd();
    }
}

不知何故,帖子参数没有被识别,我得到了回应:

"limal.info bridge error: Missing 'xml' variable in post request."

(正确的答案是 XML 数据格式错误,因为测试是无效的 XML...)

现在到下一个问题:

当我使用不同的 url 时,会发生超时异常。它挂在以下代码中的UploadValues处。(使用HttpWebRequest的另一个示例挂在GetResponse,我也尝试过。)

var url = "https://sys.efulfilment.de/rt/";
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
var alternativeAnswer = Encoding.UTF8.GetString(new WebClient().UploadValues(url, new NameValueCollection() { { "xml", "test" } }));

我在这里和其他网站上读到了类似的问题。似乎在 .NET 中使用带有 SSL 的 Http POST 是一个问题。

为什么??:(

4

0 回答 0