-3

I am making a C# program that uses the WebRequest methods, and I am wondering if these variables are called correctly:

var user = args[0];
var pass = args[1];
string site = args[2];
string prxy = args[3];
WebRequest webReq;
Uri targetUri = new Uri(site);
Credentials = user, pass;

Will this use the variables and strings in place of (site) and (user, pass), or will it use the strings literally? If that is the case, do I need to call my variables similar to batch? I.e. %site% or %user%, %pass%? Or will this work alright?

4
  • 5
    What if you give it a chance to run? Stackoverflow is a human community, not an online C# compiler
    – zerkms
    Mar 1, 2013 at 2:28
  • OK, I might just try using {0} and {1} etc. in place of the variables. Mar 1, 2013 at 2:30
  • You are not instantiating the WebRequest object. This line... Credentials = user... is not valid.
    – user885100
    Mar 1, 2013 at 2:30
  • Is this a console application, Zac? A web app? Can you provide more context? Any reason why you want to try the braces alternative you mention? Mar 1, 2013 at 2:31

3 Answers 3

1

Well Credentials = user, pass; is not valid syntax but all of the others are. It will not use the strings literally - literal strings in C# are formed by surrounding them with quotation marks:

string prxy = "MyProxyServer";
0

It will use the values passed in as parameters in args[]

For example:

args[0] = "john";
args[1] = "$ecret";
user=args[0];
pass=args[1];

req.Credentials = new NetworkCredentials(user, pass); // john, $secret

Is that what you are asking?

0
NetworkCredential myCred = new NetworkCredential(args[0],args[1]);

CredentialCache myCache = new CredentialCache();

myCache.Add(new Uri(args[2]), "Basic", myCred);

WebRequest request = WebRequest.Create(args[2]);
request.Credentials = myCache;
WebResponse response = request.GetResponse();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.