代码之家  ›  专栏  ›  技术社区  ›  Leon Barkan

不同用户的webRequest不起作用

  •  0
  • Leon Barkan  · 技术社区  · 7 年前

    当我用我的用户在我的工作站上运行它时,一切都很好,但如果我在我的工作站上以不同的用户身份运行它(用于在exe文件上更改用户shift+鼠标右键):

    using (Stream requestStream = restRequest.GetRequestStream())
    

    引发异常

    无法建立连接,因为目标计算机处于活动状态

    但是,当我将应用程序复制到另一个用户站并尝试运行它时,它工作正常,对我的用户来说也完全一样

    可能是什么?

    我的简单代码:

    JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
    Dictionary<string, object> deserializedJsonDictionary;
    
    HttpWebRequest restRequest;
    WebResponse restResponse;
    
    try
    {
        string connectionParams = "{some json params for api...}";
        restRequest = (HttpWebRequest)WebRequest.Create(URI);
        restRequest.Method = Method; //post
        restRequest.ContentType = ContentType; // application/json
    
        using (Stream requestStream = restRequest.GetRequestStream())
        {
            byte[] inputStringBytes = Encoding.UTF8.GetBytes(connectionParams);
            requestStream.Write(inputStringBytes, 0, inputStringBytes.Length);
        }
    
        using (restResponse = restRequest.GetResponse())
        {
            using (Stream responseStream = restResponse.GetResponseStream())
            {
                StreamReader rdr = new StreamReader(responseStream, Encoding.UTF8);
                string Json = rdr.ReadToEnd();
    
                deserializedJsonDictionary = (Dictionary<string, object>)jsonSerializer.DeserializeObject(Json);
            }
        }
    }
    catch(Exception ex)
    {
        throw ex;
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Leon Barkan    7 年前

    我把问题解决了

    在我们公司,我们有连接互联网的代理

    当我的用户登录时,他从GPO(包括代理)获得了所需的所有设置 当我尝试与其他用户连接时,他根本没有代理设置,这就是问题所在

    WebProxy proxy = new WebProxy("[my proxy address]", [my proxy port number]);
    proxy.BypassProxyOnLocal = false;
    request.Proxy = proxy;