c# - HttpClient and using proxy - constantly getting 407 -
here code:
httpclient client = null; httpclienthandler httpclienthandler = new httpclienthandler() { proxy = new webproxy(string.format("{0}:{1}", proxyserversettings.address, proxyserversettings.port),false), preauthenticate = true, usedefaultcredentials = false, }; this.httpclienthandler.credentials = new networkcredential(proxyserversettings.username, proxyserversettings.password); this.client = new httpclient(this.httpclienthandler); and when this:
httpresponsemessage httpresponsemessage = this.client.postasync(urltopost, new stringcontent(data, encoding.utf8, this.mediatype)).result; it throws
the remote server returned error: (407) proxy authentication required.
which not understand world of me.
the same proxy set works fine when configured in ie10 or if use httpwebrequest class instead
you're setting proxy credentials in wrong place.
httpclienthandler.credentials credentials give server after proxy has established connection. if these wrong, you'll 401 or 403 response.
you need set credentials given proxy, or refuse connect server in first place. credentials provide proxy may different ones provide server. if these wrong, you'll 407 response. you're getting 407 because never set these @ all.
// first create proxy object string proxyuri = string.format("{0}:{1}", proxyserversettings.address, proxyserversettings.port); networkcredential proxycreds = new networkcredential( proxyserversettings.username, proxyserversettings.password ); webproxy proxy = new webproxy(proxyuri, false) { usedefaultcredentials = false, credentials = proxycreds, }; // create client handler uses proxy httpclient client = null; httpclienthandler httpclienthandler = new httpclienthandler() { proxy = proxy, preauthenticate = true, usedefaultcredentials = false, }; // need part if server wants username , password: string httpusername = "?????", httppassword = "secret"; httpclienthandler.credentials = new networkcredential(httpusername, httppassword); client = new httpclient(httpclienthandler);
Comments
Post a Comment