getting exception System.Net.Http.HttpRequestException - c#

I get this exception but cant seem to find my issue:
A first chance exception of type 'System.Net.Http.HttpRequestException' occurred in System.Net.Http.dll
I am logging into a server with username and password, and the process is working just fine.
Heres my code:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
teamScreen = "https://thewebsite.com/teambeta/Login.aspx?" + "username=" +
logInUserIdString + "&password=" + logInPasswordString +
"&mobile=1&offsetHours=" + timezone;
webView1.Navigate(targetUri);
}
But I dont want to submit an app to the store with the issue not resolved.
Does anyone know what the issue is as I cant seem to track down why I am getting the exception? Thanks.

still not solved but I did fix the following as I did not have the Uri set correctly.
Here is the correct code:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
teamScreen = "https://thewebsite.com/teambeta/Login.aspx?" + "username=" +
logInUserIdString + "&password=" + logInPasswordString +
"&mobile=1&offsetHours=" + timezone;
Uri targetUri = new Uri(teamScreen);
webView1.Navigate(targetUri);
}
not sure why it actually worked before as I never set the URI. Very strange, but still getting the error.

Related

Response.Redirect not sending string

i've got a bit of a problem trying to set up a general error page in MVC.
I am handling all app errors in Global.asax.cs with the following code ->
protected void Application_Error(object sender, EventArgs e)
{
//if (Request.Url.ToString().StartsWith("http://localhost:"))
// return;
string msg;
Exception ex = Server.GetLastError().GetBaseException();
StringBuilder sb = new StringBuilder();
sb.AppendLine("Exception Found");
sb.AppendLine("Timestamp: " + System.DateTime.Now.ToString());
sb.AppendLine("Error in: " + Request.Url.ToString());
sb.AppendLine("Browser Version: " + Request.UserAgent.ToString());
sb.AppendLine("User IP: " + Request.UserHostAddress.ToString());
sb.AppendLine("Error Message: " + ex.Message);
sb.AppendLine("Stack Trace: " + ex.StackTrace);
msg = sb.ToString();
Server.ClearError();
Response.Redirect(string.Format("~/Error/Error?w={0}", msg ));
}
My problem is that i'm not getting a redirect. I see the same page URL and a blank page when i'm creating an error.
If i remove "errorMsg" and add a SIMPLE STRING, it works, redirects with the required param. Ex:
string test = "testme";
Response.Redirect(string.Format("~/Error/Error?w={0}", test));
That does redirect me to the error page with param "testme". What am i doing wrong here?
You to need escape all the parameters (UrlEncode). At the moment it is unescaped and has a whole bunch of new lines too.
Before you do that, I suggest you just append "hello world" parameter and re-display that to ensure your redirect page is working

Windows Application Log Events being 'missed' somehow with my EventLog

I've written an application, a component of which watches for Events being raised in the Windows Application Log with a certain Source and EventID in order to parse data from them. However, it appears to miss some of these events for no readily apparent reason.
I have included debug messages to try to see where the issue is - this takes the form of comments sent to a text field.
When an Entry is written to the application log, a time-stamped message is added to the debug text field, and parseApplicationLogEntry() is called.
private void eventLogApplication_EntryWritten(object sender,
System.Diagnostics.EntryWrittenEventArgs e)
{
txtDebug.Text = txtDebug.Text + "\n " + DateTime.Now.ToLongTimeString() +
+ ": Application Log has been written.";
parseApplicationLogEntry();
}
The application log entry is parsed, and the Source and EventID are looked at to determine if they are what we are looking for. A time-stamped message is added to the debug text showing the Source and EventID found.
private void parseApplicationLogEntry()
{
System.Diagnostics.EventLog log = new System.Diagnostics.EventLog("Application");
int entry = log.Entries.Count - 1;
string logMessage = log.Entries[entry].Message;
string logSource = log.Entries[entry].Source;
string logEventID = log.Entries[entry].InstanceId.ToString();
log.Close();
txtDebug.Text = txtDebug.Text + "\n " + DateTime.Now.ToLongTimeString() +
": Application Log Source is " + logSource;
txtDebug.Text = txtDebug.Text + "\n " + DateTime.Now.ToLongTimeString() +
": Application Log EventID is " + logEventID;
if (logSource == "ExpectedSource" & logEventID == "ExpectedEventID")
{
// Do stuff
}
}
The behaviour is as expected much of the time, however sometimes there is very odd behaviour.
For example, 13 logs were written to the application log. 3 with the looked-for source, and 10 with another source. The debug text shows 13 entries were seen, all with the unfamiliar source...
I'm not sure where to go from here.
There is no need to access the EventLog in this way to review the newest entries.
Instead of calling a method to iterate through the EventLog each time a new Entry is written, it is simpler (and safer) to access the Entry more directly using the event handler which triggers each time an Entry is written.
private void eventLog_Application_EntryWritten(object sender, EntryWrittenEventArgs e)
{
// Process e.Entry
}

ASP.NET can't create Windows Event Log even as Administrator

I'm working on an ASP.NET web application in Visual Studio 2013 and as part of the main error routine, I want it to store the errors in a Windows Event Log. However, every time I run the code, the log never gets created even when I run Visual Studio as Administrator. Would turning off UAC fix this? Code is below. Any help is appreciated. Thanks.
public static void ErrorRoutine(Exception e, string obj, string method)
{
EventLog.Delete("LogName"); // uncomment this line to delete log
EventLog log = new EventLog();
log.Source = "SourceName";
log.Log = "LogName";
if (e.InnerException != null)
{
log.WriteEntry("Error in Models, object = " + obj + ", method = " + method + ", inner exception = " +
e.InnerException.Message, EventLogEntryType.Error);
throw e.InnerException;
}
else
{
log.WriteEntry("Error in Models, object = " + obj + ", method = " + method + ", message = " + e.Message,
EventLogEntryType.Error);
throw e;
}
}
Edit: I get that this is probably not best practice but this project is actually an assignment for college and this is the exact code we were told to write, as well as being told to run VS as Administrator. I just need to find a way to make work really.

Include full trace with stack trace / error output c#

I'm using the following code in my global.asax file:
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
Exception objErr = Server.GetLastError().GetBaseException();
string err = "Error Caught in Application_Error event<hr />" + "Current User: " + HttpContext.Current.User.Identity.Name + "<hr />" +
"Error in: " + Request.Url.ToString() +
"<hr />Error Message:" + objErr.Message.ToString() +
"<hr />Stack Trace:" + objErr.StackTrace.ToString();
//EventLog.WriteEntry("Sample_WebApp", err, EventLogEntryType.Error);
doEmail.sendEmail("Application Error - Curato (" + System.Configuration.ConfigurationManager.AppSettings["OrganisationName"].ToString() + ")", err);
// We do not want the error handled by the web.config as well, so clear the error.
Server.ClearError();
// Now redirect ourselves...
Response.Redirect("~/error.aspx?aspxerrorpath=" + Server.UrlEncode(Request.Url.PathAndQuery));
}
This sends the stackTrace and current user to an admin email address, and redirects the user to a friendly error page.
How would I be able to include the full Trace information in err? That is, the same information as if I had set Trace="True" in the <%# Page directive.
I'm exploring the Trace object - and found how to read it from a 3rd party reader how to read trace but I'd like to read it just in native code.
You're already getting the full StackTrace of the exception, I believe you're looking at the wrong Exception object:
I think your error is in this line:
Exception objErr = Server.GetLastError().GetBaseException();
I think the correct one should be:
Exception objErr = Server.GetLastError();
You can also inspect the exception to see if there are InnerException:
if (objErr.InnerException != null)
err = err + "<hr/>Message:" + objErr.InnerException.Message;
See this question also:
Application_Error - GetLastError() or GetLastError().GetBaseException()

submit sitemap to the google

System.Net.WebRequest reqGoogle = System.Net.WebRequest.Create("http://www.google.com/webmasters/tools/ping?sitemap=" + HttpUtility.UrlEncode("http://www.shree/SiteMap/'" + PortalName + "'/sitemap.xml"));
reqGoogle.GetResponse();
Code work fine when google is not blocked.For some region my administrator block the goole.After blocking google code gives an error. How to check the site first if it is blocked or not.plz help.
like this(?):
try
{
System.Net.WebRequest reqGoogle = System.Net.WebRequest.Create("http://www.google.com/webmasters/tools/ping?sitemap=" + HttpUtility.UrlEncode("http://www.shree/SiteMap/'" + PortalName + "'/sitemap.xml"));
reqGoogle.GetResponse();
}
catch(WebException ex)
{
MessageBox.Show("Google is blocked");
}

Categories