Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Ubuntu上使用HttpWebResponse和Mono清空响应数据_C#_Xml_Ubuntu_Mono_Httpwebresponse - Fatal编程技术网

C# 在Ubuntu上使用HttpWebResponse和Mono清空响应数据

C# 在Ubuntu上使用HttpWebResponse和Mono清空响应数据,c#,xml,ubuntu,mono,httpwebresponse,C#,Xml,Ubuntu,Mono,Httpwebresponse,我正试图用mono在Ubuntu上运行一个.NET程序 程序连接到远程服务器API并获取XML字符串作为响应。 下面是负责此任务的函数的简化版本 using System.Net; static string GetProducts(string clientID, string apiKey, string url) { HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest; r

我正试图用mono在Ubuntu上运行一个.NET程序

程序连接到远程服务器API并获取XML字符串作为响应。 下面是负责此任务的函数的简化版本

 using System.Net;

 static string GetProducts(string clientID, string apiKey, string url)
    {
        HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
        req.Credentials = new NetworkCredential(clientID, apiKey);
        req.ContentType = "application/x-www-form-urlencoded";

        string result = string.Empty;

        using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
        {
            StreamReader reader = new StreamReader(resp.GetResponseStream());
            result = reader.ReadToEnd();
        }

        return result;
    }
它在我的Windows8.1机器上运行得非常好。但我的目标是在Ubuntu VPS上运行它。 我正在使用mono来实现这一点。程序运行,但在尝试解析下载的XML字符串时出现异常停止

[ERROR] FATAL UNHANDLED EXCEPTION: System.Xml.XmlException: Document element did not appear.  Line 1, position 1.
通过一点探索,我发现程序实际上并没有得到XML响应,而是生成了一个空字符串。这很奇怪,因为没有抛出连接错误

我以前有过一些使用mono和Ubuntu的经验,但从未遇到过这样的问题

这可能与Ubuntu服务器或mono有关吗?还是在代码本身


对此有何想法?

最后我找到了问题所在,并找到了某种解决办法。首先,我用Perl编写了一个简单的代码来查看问题所在:mono或linux服务器本身

#!/usr/bin/perl 
use LWP::UserAgent 6;
my $ClientID = "id";
my $APIKey = "key";
my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 });
my $req = HTTP::Request->new(GET => 'https://server.url');
$req->authorization_basic($ClientID, $APIKey);
my $res = $ua->request( $req );
print "Status: " . $res->status_line . "\n";
print "Contents: ". $res->content . "\n";
Perl代码工作正常,为我提供了请求状态并打印出内容。但是为了让它工作,我必须禁用服务器安全检查
LWP::UserAgent->new(ssl\u opts=>{verify\u hostname=>0})
,否则它将无法工作。这让我思考。也许这就是为什么我的C代码中没有任何响应数据。因为安全检查失败。经过进一步测试,我得到了我的确认,这确实是我的情况。我无法找到ssl安全检查失败的原因,所以我决定完全忽略检查,就像我在Perl代码中所做的那样。我做了一些研究,发现了这个线索。我刚刚把这段代码添加到我的函数中,它就开始工作了

System.Net.ServicePointManager.ServerCertificateValidationCallback +=
delegate(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
                    System.Security.Cryptography.X509Certificates.X509Chain chain,
                    System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
    return true; // **** Always accept
};
我仍然不知道为什么证书检查在linux上失败。但至少,通过忽略检查,它可以在Windows和Linux上运行。以下是最终代码:

using System.Net;

static string GetProducts(string clientID, string apiKey, string url)
{
    HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
    req.Credentials = new NetworkCredential(clientID, apiKey);
    req.ContentType = "application/x-www-form-urlencoded";

    System.Net.ServicePointManager.ServerCertificateValidationCallback +=
    delegate(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
                    System.Security.Cryptography.X509Certificates.X509Chain chain,
                    System.Net.Security.SslPolicyErrors sslPolicyErrors)
    {
        return true;
    };

    string result = string.Empty;

    using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
    {
        StreamReader reader = new StreamReader(resp.GetResponseStream());
        result = reader.ReadToEnd();
    }

    return result;
}

您可以尝试使用NetTool之类的工具来分析ubuntu的流量,然后将其与您在windows中的Fiddler上看到的流量进行比较。也许你在windows中依赖的某些默认标题/设置可能需要在linux中显式设置。@cgotberg经过一些测试后,我发现在windows和ubuntu中发送的标题有点不同。在Windows上
内容类型:应用程序/x-www-form-urlencoded授权:基本MjY2MjQyOmFmNDhlZjUwZDFiM2Y5MWRiZmE5ZTAwYTNjM2ZhNjg3
在Ubuntu上
内容类型:应用程序/x-www-form-urlencoded连接:保持活动状态
看起来Ubuntu缺少授权行。但我自己并没有在Windows上的任何地方设置它。这有什么重要的吗?很可能就是问题所在。这一行设置了授权,这似乎是Ubuntu中不起作用的。请求凭证=新的网络凭证(clientID,apiKey);不幸的是,我没有任何经验来解释为什么会这样。可能可以尝试类似的解决方案@谢谢你的回复。的确,请求凭证似乎不适用于Ubuntu。根据您发布的解决方案,我手动添加了授权标题。现在,每个系统中的标题或多或少是相同的。不幸的是,这个问题仍然存在。我在Ubuntu上没有收到任何响应数据。这可能是服务器问题而不是程序问题吗?这可能是很多问题,尽管您可能认为对web服务服务器的相同请求会导致相同的响应,无论它们来自何处。当你从ubuntu打电话时,你有没有得到任何回应?如果你没有收到回复,可能是防火墙(即服务器拒绝向ubuntu机器ip发送响应)。如果你得到一个回应,但它是空的,那么问题就更难解决了。