Urgent Help - How to update HTTP/HTTPS proxy settings programmitically using SystemConfiguration framework??
Urgent Help - How to update HTTP/HTTPS proxy settings programmitically using SystemConfiguration framework??
- Subject: Urgent Help - How to update HTTP/HTTPS proxy settings programmitically using SystemConfiguration framework??
- From: jdc ram <email@hidden>
- Date: Fri, 26 Mar 2004 06:09:45 -0800 (PST)
Hi All,
Greetings. Iam new to this list and also to MacWorld
of programming. I would like to know if it is possible
to update the proxy settings on a MacOS X system
programmitically?. What I wanted is to write a program
which will modify the HTTP/HTTPS proxy settings to a
known IP/port. Then all the Web-traffic from my Safari
browser should flow through that proxy. As a first
step using code segments found in Web I was able to
retrieve the proxy values but Iam unable to modify the
same. Any help here is greatly appreciated. Iam
enclosing the code I used to read the proxy values for
your reference(currently this code reads only HTTPS
proxy settings...)
Thanks,
Ram
CODE begins....
------------------------
#include <SystemConfiguration/SystemConfiguration.h>
Boolean GetHTTPSProxySetting(char *host, size_t
hostSize, UInt16 *port)
// Returns the current HTTPS proxy settings as a C
string
// (in the buffer specified by host and hostSize)
and
// a port number.
{
Boolean result = 0;
CFDictionaryRef proxyDict;
CFMutableDictionaryRef proxyDictSet;
CFNumberRef enableNum;
int enable;
CFStringRef hostStr;
CFNumberRef portNum = 0;
int portInt;
SCDynamicStoreRef proxyStore;
assert(host != NULL);
assert(port != NULL);
// Get the dictionary.
printf("hostSize=%d\n",hostSize);
proxyStore =
SCDynamicStoreCreate(NULL,CFSTR("NetScaler"),NULL,NULL);
proxyDict = SCDynamicStoreCopyProxies(proxyStore);
result = (proxyDict != NULL);
// Get the enable flag. This isn't a CFBoolean,
but a CFNumber.
if (result) {
enableNum = (CFNumberRef)
CFDictionaryGetValue(proxyDict,
kSCPropNetProxiesHTTPSEnable);
result = (enableNum != NULL)
&& (CFGetTypeID(enableNum) ==
CFNumberGetTypeID());
}
if (result) {
result = CFNumberGetValue(enableNum,
kCFNumberIntType,
&enable) && (enable != 0);
}
// Get the proxy host. DNS names must be in
ASCII. If you
// put a non-ASCII character in the "Secure Web
Proxy"
// field in the Network preferences panel, the
CFStringGetCString
// function will fail and this function will
return false.
if (result) {
hostStr = (CFStringRef)
CFDictionaryGetValue(proxyDict,
kSCPropNetProxiesHTTPSProxy);
result = (hostStr != NULL)
&& (CFGetTypeID(hostStr) ==
CFStringGetTypeID());
}
if (result) {
result = CFStringGetCString(hostStr, host,
(CFIndex) hostSize,
kCFStringEncodingASCII);
}
// Get the proxy port.
if (result) {
portNum = (CFNumberRef)
CFDictionaryGetValue(proxyDict,
kSCPropNetProxiesHTTPSPort);
result = (portNum != NULL)
&& (CFGetTypeID(portNum) ==
CFNumberGetTypeID());
}
if (result) {
result = CFNumberGetValue(portNum,
kCFNumberIntType, &portInt);
}
if (result) {
*port = (UInt16) portInt;
}
//proxyDictSet = SCDynamicStoreCopyProxies(NULL);
proxyDictSet =
CFDictionaryCreateMutableCopy(NULL,0,proxyDict);
CFDictionarySetValue(proxyDictSet,
kSCPropNetProxiesHTTPSProxy,CFSTR("127.0.0.1"));
enable = 1;
CFDictionarySetValue(proxyDictSet,
kSCPropNetProxiesHTTPSEnable,CFNumberCreate(NULL,kCFNumberLongType,&enable));
hostStr = (CFStringRef)
CFDictionaryGetValue(proxyDictSet,
kSCPropNetProxiesHTTPSProxy);
result = CFStringGetCString(hostStr, host,
(CFIndex) hostSize,
kCFStringEncodingASCII);
printf("HTTPS-Set proxy host = %s\n",host);
printf("now we try the new thing...\n");
//if(SCDynamicStoreSetValue(NULL,kSCPropNetProxiesHTTPSProxy,CFSTR("127.0.0.1")))
{
if(SCDynamicStoreSetValue(proxyStore,kSCPropNetProxiesHTTPSProxy,proxyDictSet))
{
printf("store updated successfully...\n");
}else {
printf("store NOT updated successfully...\n");
printf("Error is %s\n",SCErrorString(SCError()));
}
// Clean up.
if (proxyDict != NULL) {
CFRelease(proxyDict);
}
if ( ! result ) {
*host = 0;
*port = 0;
}
return result;
}
int main(int argc, char** argv) {
UInt16 port;
char host[100];
GetHTTPSProxySetting(host,sizeof(host),&port);
printf("HTTPS proxy host = %s\n",host);
printf("HTTPS proxy host = %d\n",port);
}
__________________________________
Do you Yahoo!?
Yahoo! Finance Tax Center - File online. File on time.
http://taxes.yahoo.com/filing.html
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.