Remote server (407) Proxy Authentication Required - c#

I want to connect to an http web page , but everytime it enters in catch and returns error Remote server (407) Proxy Authentication Required. why? I used the same code in another website project and it worked.
WebProxy wp = new WebProxy("**");
wp.Credentials = new NetworkCredential("**", "**");
StringBuilder sbXmlText = new StringBuilder();
try
{
WebClient MyWebClient = new WebClient();
MyWebClient.Proxy = wp;
Stream TheStream = MyWebClient.OpenRead("http://www.bnr.ro/nbrfxrates10days.xml");
StreamReader TheStreamReader = new StreamReader(TheStream);
while (!TheStreamReader.EndOfStream)
sbXmlText.Append((char)TheStreamReader.Read());
}
catch (Exception ex)
{
mail_send_bnr(ex.ToString(), "Error on connecting to bnr", "test");
}

Related

Soap request using service reference

I am trying to call an external soap request but i keep getting the error below:
System.ServiceModel.FaultException: java.lang.NullPointerException
I can't figure out what is lacking here, i have check the wsdl and didn't find any parameters that are mandatory.
SoapService.queryWebServiceSoapPortClient client = new SoapService.queryWebServiceSoapPortClient();
client.ClientCredentials.UserName.UserName = "";
client.ClientCredentials.UserName.Password = "";
SoapService.qwsInput query1 = new SoapService.qwsInput();
SoapService.queryResponse response = new SoapService.queryResponse();
query1.pass = Password;
query1.queryId = queryId;
query1.qwsInputParams = "something";
query a = new query();
a.queryInput = query1;
try
{
response = client.query(a);
}
catch (Exception error) {
var b = error.ToString();
}
I eventually contacted the soap provider and they figure out the account i was using to connect to the soap service was not working for some reason. The above code works perfectly with the new account.

Trying to Upload Images on Server using Webclient

I am trying to save my images using weblclient Object.
Here it is my code..
try
{
WebClient client = new WebClient();
NetworkCredential nc = new NetworkCredential("****", "*****");
Uri addy = new Uri(#"\\104.199.191.5\Images\");
client.Credentials = nc;
byte[] arrReturn = client.UploadFile(addy, "Post", #"E:\free-html-admin-templates.JPG");
}
catch (Exception ex)
{
}
Right now I am getting error Network path not found..
Where am I wrong ??

Service HTTP Post is receiving "Existing Connection Was Forcibly Closed"

I have a windows service that I am trying to get running on a server. It just posts data to a php page and has further logic based on the response back. This works 100% on my machine and 100% on the server WHEN Anonymous Authenication is turned on for the website(IIS).
What I want to accomplish is to run the site with Windows Authenication turned ON and have my service still work... except when I try this I receive a nice EventLog entry.
"System.IO.IOException: Unable to read data from the transport
connection: An existing connection was forcibly closed by the remote
host"
I am running the service under an account that has all required permissions. So I understand that "CredentialCache.DefaultCredentials" should use this account for authentication( I am not receiving a 401 error ,but not ruling it out )
Below is my HttpPost code, I have set quite a few options from other posts trying to find a solution.
string postData = xml;
//--------------
HttpRequestCachePolicy policy = new HttpRequestCachePolicy MontgP02(HttpRequestCacheLevel.NoCacheNoStore);
HttpWebRequest.DefaultCachePolicy = policy;
byte[] buffer = Encoding.UTF8.GetBytes(postData);
//Initialisation
System.Net.ServicePointManager.Expect100Continue = false;
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(url);
//Credentials
WebReq.UseDefaultCredentials = true;
WebReq.PreAuthenticate = true;
WebReq.Credentials = CredentialCache.DefaultCredentials;
//Request Settings
WebReq.ProtocolVersion = HttpVersion.Version11;
//WebReq.ServicePoint.ConnectionLimit = 1;
WebReq.Method = "POST";
WebReq.ContentType = "application/x-www-form-urlencoded";
//The length of the buffer
WebReq.ContentLength = buffer.Length;
//Set timeout
WebReq.Timeout = 25000;
//Use Proxy or Not
if (useproxy == "ON")
{
WebProxy myProxy = new WebProxy();
// Create a new Uri object.
Uri newUri = new Uri(proxy);
// Associate the new Uri object to the myProxy object.
myProxy.Address = newUri;
WebReq.Proxy = myProxy;
}
try
{
//Send Data
using (Stream requestStream = WebReq.GetRequestStream())
{
// write to stream
//write, and close.
requestStream.Write(buffer, 0, buffer.Length);
}
}
catch (WebException ex) {
MessageLog("ERROR: Sending Data : " + ex.ToString() + ":", "ERROR");
}
//Get Response
try
{
using (System.Net.WebResponse response = WebReq.GetResponse())
{
Stream Answer = response.GetResponseStream();
// read from stream
StreamReader _Answer = new StreamReader(Answer);
PostResponse = _Answer.ReadToEnd();
}
}
catch (WebException ex)
{
MessageLog("ERROR: Receiving Response : " + ex.ToString() + ":" , "ERROR");
}
}
catch (WebException ex)
{
MessageLog("ERROR Posting " + ex.ToString() + ":" + PostResponse, "ERROR");
}
}
I understand from the error that the server is cutting the connection before I can complete the request. This happens everytime on the server negating thoughts of timeouts I would think.
Having looked around other fourms, I am at a loss , having tried other variations etc the fact that it does work fine with authenication off leads me to think it may be a setting in IIS or something else down the authenication route.
Any insight welcome

get html source through proxy

Hi how do I get the source of an html page through a proxy. When I use the code below I get an error saying "Proxy Authentication Required." and I have to go through a proxy.
Dim client As New WebClient()
Dim htmlCode As String = client.DownloadString("http://www.stackoverflow.com")
Then use a proxy that does not need authentication
see here for more info
http://msdn.microsoft.com/en-us/library/system.net.webclient.proxy.aspx
string source = GetPageSource("http://www.stackoverflow.com");
private string GetPageSource(string url)
{
string htmlSource = string.Empty;
try
{
System.Net.WebProxy myProxy = new System.Net.WebProxy("Proxy IP", 8080);
using (System.Net.WebClient client = new System.Net.WebClient())
{
client.Proxy = myProxy;
client.Proxy.Credentials = new System.Net.NetworkCredential("username", "password");
htmlSource = client.DownloadString(url);
}
}
catch (WebException ex)
{
// log any exceptions
}
return htmlSource;
}

Get location of client machine using ip address

I am trying to get the location of client machine using ip address. Client can access the internet only if
he/she provide the proxy authenication.
Let us say client need to access the 'www.google.com' on the browser then immediately Authenication Required
prompt window open and then client enter his/her username and password. But it is possible the few users does
not required the provide the authenication in order to access internet.
This segment of code does not helped me...
string url = "http://freegeoip.net/xml/";
WebClient wc = new WebClient();
WebProxy proxyObj = new WebProxy("http://freegeoip.net/xml/");
proxyObj.Credentials = CredentialCache.DefaultCredentials;
Uri uri = new Uri(url);
MemoryStream ms = new MemoryStream(wc.DownloadData(uri));
XmlTextReader rdr = new XmlTextReader(url);
XmlDocument doc = new XmlDocument();
ms.Position = 0;
doc.Load(ms);
ms.Dispose();
In the above code if i add network credential instance with username, password and domain then it's work perfectly
Instead of providing the default net credential in code itself, I need to get the username and password from the users(client
machine).
My question is how to prompt the Authentication Required Window and get the username and password to load the download from url
I would be glad if someone throw light on this issue...
Edit: Somehow basic authentication window prompt and now i can get the username and password which can use for credential
try
{
var reg = HttpContext.Current.Request;
if (!String.IsNullOrEmpty(reg.Headers["Authorization"]))
{
var cred = System.Text.ASCIIEncoding.ASCII.GetString(Convert.FromBase64String(Request.Headers["Authorization"].Substring(6))).Split(':');
var user = new { Name = cred[0], Pass = cred[1] };
string url = "http://freegeoip.net/xml/";
WebClient wc = new WebClient();
WebProxy wProxy = new WebProxy();
ICredentials crd;
crd = new NetworkCredential("'" + cred[0] + "'", "'" + cred[1] + "'");
wProxy = new WebProxy("myproxy", true, null, crd);
wc.Proxy = wProxy;
Uri uri = new Uri(url);
string content = wc.DownloadString(uri);
}
else
{
try
{
//var reg = HttpContext.Current.Request;
if (String.IsNullOrEmpty(reg.Headers["Authorization"]))
{
var res = HttpContext.Current.Response;
res.StatusCode = 401;
res.AddHeader("WWW-Authenticate", "Basic realm = \"freegeoip\"");
//res.End();
}
}
catch (Exception ex)
{
}
}
}
catch(Exception ex)
{
}
But Still It throwing the "Unable to connect to the remote server"

Categories