I am not able to use Webrequest in a windows service. It fails with error ""Unable to connect to the remote server".
WebRequest request = WebRequest.Create(url);
NetworkCredential nc = new NetworkCredential("myuname","mypassword","mydomain");
request.Proxy.Credentials = nc;
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
}
catch (WebException ex)
{
//ex.Message is "Unable to connect to the remote server"
}
The code works perfectly fine if it is a console application.
Could someone please tell if there is a fix?
You might want to try specifying the proxy server on the request object you've created. When you run the console application I believe that the system looks up your IE configuration for you and the proxy may be set. If the service is running under an account other than yours it might not have the proxy set.
I was facing the same problem. After lots of R&D a solution worked for me.
Instead of:
WebResponse responseObject = requestObject.GetResponse();
I wrote:
WebResponse responseObject = null;
responseObject = requestObject.GetResponse();
and it worked fine.
I am also looking for the exact reason behind this behavior.
Related
I need to call HTTPS POST service from WEB API method.
I am using HttpWebRequest to call this API, however i am getting error as
"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond x.x.x.x:443"
When I tried to call this API in Windows Form then it works well.
HttpWebRequest r = null;
HttpWebResponse rsp = null;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
r = (HttpWebRequest)System.Net.WebRequest.Create("url");
String d = "Serialize JSON"
r.Method = "POST";
r.ContentType = "application/json";
StreamWriter wr = new StreamWriter(r.GetRequestStream());
wr.WriteLine(d);
wr.Close();
rsp = (HttpWebResponse)r.GetResponse();
StreamReader reader = new StreamReader(rsp.GetResponseStream());
String dd = reader.ReadToEnd();
Might be because you are trying to use SSL, from the last part - x.x.x.x:443. My understanding is that 443 is used for SSL. Happy for someone to correct me if I incorrect.
Have a look at the following, this may help you out:
https://learn.microsoft.com/en-us/aspnet/web-api/overview/security/working-with-ssl-in-web-api
So I've been getting into WebRequests recently and something that I found pretty interesting was making a connection through a proxy.
I looked at some blog posts for some code to get a general idea on how it worked and the most recent one would be this code snippet right here.
private static void requestProxy()
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://google.com");
WebProxy myproxy = new WebProxy("77.121.11.33", 1080);
myproxy.BypassProxyOnLocal = false;
request.Proxy = myproxy;
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
string content = sr.ReadToEnd();
Debug.Print(content);
Console.WriteLine(content);
}
Console.ReadLine();
}
I tried making a request using that but it seems that it's throwing me an error everytime I try to use a proxy. However when I am not using a proxy it's not throwing me any errors, so I am assuming that the problem lays within the proxy. I tried using different ones but no go.
What I am doing wrong here and whats going on? How do I make a connection with a proxy properly?
Error message
System.Net.WebException: 'An error occurred while sending the request.
The server returned an invalid or unrecognized response'
https://imgur.com/ThxNWzb
i made a program using winforms that reproduce the execution of Reports.
meaning, i input dates: from.... to.... and the code Rerun the reports.
i used:
System.Diagnostics.Process.Start(Url.ToString());
and it works well, but it opens the IE,
now i want to run the url behind the scenes without displaying it in browser.
i tried:
try
{
WebRequest myRequest = WebRequest.Create(Url.ToString());
myRequest.UseDefaultCredentials = true;
HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
var statusResponse = response.StatusDescription;
Stream dataStream = response.GetResponseStream();
StreamReader readerr = new StreamReader(dataStream);
string responseFromServer = readerr.ReadToEnd();
var responseServer = responseFromServer;
}
response.Close();
}
it just doesnt work!
what did i do wrong?
thanks
OK,
i found the problem.
a had an error:
"{"The remote server returned an error: (500) Internal Server Error."}
i just did not display it.
thanks
The reccomended approach for triggering report renderings via code is the via the SOAP api - https://msdn.microsoft.com/en-us/library/ms154052.aspx
While debugging this code fragment ,this error(Unable to connect remote server) occurs.I showed where error occured in comment line
private static string GetWebText(string url)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.UserAgent = "A .NET Web Crawler";
WebResponse response = request.GetResponse();//errors occured here
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string htmlText = reader.ReadToEnd();
return htmlText;
}
You should try to figuere out what is wrong, it can be wrong url or something that goes wrong with the request / response. In order to look at the traffic behind your application I would suggest using Fiddler for checking what happens behind the scene.
I'm trying to write a small desktop application that will monitor a website through their API. I've never really done much work with APIs or WebRequests, so I'm unsure as to how to proceed with this error. After generating a requests, I try to GET the response from the page, but this returns a 404 error, even though if I navigate to the same URL in my browser it works fine. I have no idea how to get around this, and the research I've done on people who had the same problem hasn't helped me to understand it. Could someone explain to me what is happening at a basic level? My code has been posted below:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
string sURL;
sURL = #"https://habitrpg.com/api/v1/user";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sURL);
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (Exception ex)
{
resultText.Text = "Oh no!";
}
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
string responseData = readStream.ReadToEnd();
}
You should ignore https certificate, look at this question:
How do I use WebRequest to access an SSL encrypted site using https?