Search code examples
c#.netnetwork-programminghttpwebrequest

webRequest doen't work from different user


I build a small winform application that use some API for sending sms messages, when i run it on my station with my user everything works great but if i run it on my station but as different user (for changing the user on that exe file shift + right mouse button):

using (Stream requestStream = restRequest.GetRequestStream())

throws exception

No connectuion could be made because the target machine actively refused if (ip address)

but when i copy the app to the other user station and try to run it it's work fine and doing exactly the same for my user

the problem is that the code should be integrated to application that use server that have wcf service that will send the sms's and the user that in iis applicationpool throwing that same exception

what could it be ?

my simple code:

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;
}

Solution

  • I get the problem solved

    in our company we have proxy for connecting to the internet

    when my user was logged on he has all the settings he needed from the GPO (include proxy) when i tried to connect with other user he simply didn't have the proxy settings and that was the issue

    what i did is simply added proxy to the code:

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