简体   繁体   English

C#无法从网站或服务发出http请求

[英]C# can't make http request from website or service

I am trying to write a simple POST request to google-analytics server, here is my code : 我正在尝试向google-analytics服务器编写一个简单的POST请求,这是我的代码:

        using (var client = new System.Net.WebClient())
        {


            var values = new System.Collections.Specialized.NameValueCollection();
            //values["v"] = "1";
            //values["t"] = "event";
            //values["tid"] = trackingID;
            //values["cid"] = clientID;
            //values["ec"] = eventCategory.ToString();
            //values["ea"] = eventAction.ToString();
            //values["el"] = eventAction.ToString();

            var endpointAddress = "http://www.google-analytics.com/collect";
            var response = client.UploadValues(endpointAddress, values);

            var responseString = System.Text.Encoding.Default.GetString(response);
        }

This code works fine in a console application, but not on a website application (hosted on IIS or run on Visual Studio 2013) or in a WCF (likewise). 此代码在控制台应用程序中工作正常,但在网站应用程序(托管在IIS上或在Visual Studio 2013上运行)或WCF(同样)中不起作用。 I checked using 我检查了使用

WindowsIdentity.GetCurrent()

in both the site, the WCF service and the application, everytime the DOMAIN and USERNAME are my own, so I don't think that is the problem. 在站点,WCF服务和应用程序中,每次DOMAIN和USERNAME都是我自己的,所以我认为这不是问题。 I have tried using .NET impersonation without success. 我尝试使用.NET模拟无法成功。

I've tried setting the application pool identity to my user, ApplicationPoolIdentity or NetworkService, without success. 我尝试将应用程序池标识设置为我的用户ApplicationPoolIdentity或NetworkService,但没有成功。 I've also tried changing the authentication mode to AnonymousUser or Windows Authentication. 我也尝试过将身份验证模式更改为AnonymousUser或Windows身份验证。 I've tried changing the physical access path, without success. 我尝试更改物理访问路径,但没有成功。 I'm at work behind a proxy, at home I've tried it and it worked well. 我正在代理的背后工作,在家里我已经尝试过了,而且效果很好。

Does anyone has an idea as to why it doesn't work ? 有谁知道为什么它不起作用?

Try supplying the proxy details when making the request. 发出请求时,尝试提供代理详细信息。 Assuming you are behind a proxy. 假设您在代理后面。

    using (var client = new System.Net.WebClient())
    {
        WebProxy proxy = new WebProxy("localproxyIP:8080", true);
        proxy.Credentials = new NetworkCredential("domain\\user", "password");
        WebRequest.DefaultWebProxy = proxy;
        client.Proxy = proxy;

        var values = new System.Collections.Specialized.NameValueCollection();
        //values["v"] = "1";
        //values["t"] = "event";
        //values["tid"] = trackingID;
        //values["cid"] = clientID;
        //values["ec"] = eventCategory.ToString();
        //values["ea"] = eventAction.ToString();
        //values["el"] = eventAction.ToString();

        var endpointAddress = "http://www.google-analytics.com/collect";
        var response = client.UploadValues(endpointAddress, values);

        var responseString = System.Text.Encoding.Default.GetString(response);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM