|
Is this an error being displayed by the website you're requesting, rather than an exception thrown from your code?
Perhaps the server-side code depends on a header which you're not sending - for example, the user-agent[^].
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
My code (the source code I included in my first post) works just fine (in the sense that it doesn't throw any exceptions), it returns a string with html as expected. The problem is the html itself, when I look at it inside Notepad then I see the System.NullReferenceException.
modified 18-May-20 7:39am.
|
|
|
|
|
So as I said, the server-side code is depending on a header that you're not sending with your C# code. The most likely culprit would be the user-agent.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Using the example code posted on the web page you linked to in your post, I was able to modify my code to the following:
public static GenericStatusType LoadHttpPageWithBasicAuthentication(string url, string username, string password)
{
GenericStatusType status = new GenericStatusType(GenericStatusCode.OK, null, "", "");
try
{
Uri myUri = new Uri(url);
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(myUri);
myHttpWebRequest.UserAgent = ".NET Framework Test Client";
NetworkCredential myNetworkCredential = new NetworkCredential(username, password);
CredentialCache myCredentialCache = new CredentialCache();
myCredentialCache.Add(myUri, "Basic", myNetworkCredential);
myHttpWebRequest.PreAuthenticate = true;
myHttpWebRequest.Credentials = myCredentialCache;
HttpWebResponse myWebResponse = null;
try
{
myWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
}
catch (WebException e)
{
status.errorHeading = "Error Reading Web Page";
status.errorMsg = "A WebException occurred while trying to read from the following URL: " + url + ". Technical information: " + e.Message;
status.statusCode = GenericStatusCode.ERROR;
if (myWebResponse != null)
{
myWebResponse.Close();
}
return status;
}
Stream responseStream = myWebResponse.GetResponseStream();
StreamReader myStreamReader = new StreamReader(responseStream, Encoding.Default);
string pageContent = myStreamReader.ReadToEnd();
responseStream.Close();
myWebResponse.Close();
status.value = pageContent;
}
catch (Exception e)
{
status.errorHeading = "Error Reading Web Page";
status.errorMsg = "An error occurred while trying to read from the following URL: " + url + ". Technical information: " + e.Message;
status.statusCode = GenericStatusCode.ERROR;
}
return status;
}
As far as I can tell, this works great! For some reason, some values get changed (for example, ;_hide=;title;" id="x23866"> becomes ;_hide=;title;" id="x1295">), but that doesn't matter in my use case. Thanks for solving this!
|
|
|
|
|
The HTML code one gets often depends on the device the client uses (e.g. large desktop vs small mobile phone), therefore setting a reasonable useragent is pretty important, as Richard already told you.
Example: my desktop Windows10/Chrome browser uses this user-agent (a single rather long line of text):
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36
|
|
|
|
|
Sometimes, when you have an exception on your code, that is catched and rethrown and that you press F5 (continue) the debugger will stop stop obstinately several times in a row in the same spot (I found that more prevalent in Web application) and you have to press F5 F5 F5.... but not too many times because, sometimes, the 8th or 9th time is in another relevant user method...
Is there a way to have the debugger stop on all exception, but skip some method altogether?!
I tried to decorate my relevant method with
[System.Diagnostics.DebuggerHidden]
[System.Diagnostics.DebuggerStepThrough]
[System.Diagnostics.DebuggerNonUserCode]
[System.Diagnostics.DebuggerStepperBoundary]
The closest I was to something was with DebuggerHidden , but it still stops like 8 or 9 times on my method (it's an ExceptionFilter ), just not select it...
modified 17-May-20 20:41pm.
|
|
|
|
|
When I start a new Windows Forms project in Visual Studio it always places the form at the upper left of the screen which seems stupid to me to put it there.
I'm looking for a way to move it to center screen.
As far as I know adjusting the Properties only affects run-time position of the form.
|
|
|
|
|
This is not a programming issue. You need to talk to Microsoft, it's their product.
|
|
|
|
|
I had to check this out, and I see the same thing.
But to me it seems quite natural. When I create a new text file, I start writing in the upper left hand corner, too.
All sorts of boes and forms, frames and whatever to be filled in, I feel it natural to fill left to rigth, top to bottom. If I want to drag a box larger, I alway drag the lower right corner (so the old contents stays in the same positions, unless it is flowing, of course.
Personal taste varies. But I am starting to ask myself why the window is centered when you work with a WPF app. I think upper left would be better.
|
|
|
|
|
Were you looking for the StartPosition property of your form? You can set that to "CenterScreen".
|
|
|
|
|
That only affects what your form looks like when you're at run-time. I was wondering if I can get the form positioned closer to the center of the screen at design-time, but I don't think that can be changed.
|
|
|
|
|
No, you can't.
If you want that, switch to WPF, but you're going to have to rewrite your entire app to do that.
|
|
|
|
|
I can see good reasons to switch to WPF, but this is not one of them.
|
|
|
|
|
class B
{
public int z = 3;
public string s = "bbb";
}
class A
{
public int[] x = { 1, 2 };
public string str = "asdf";
public float y = 2.0f;
public B b = new B();
}
How to get list of all variables and their values for entity A a = new A() using C# Reflection ?
|
|
|
|
|
The variables are known as fields. If you do Type.GetFields for the type, you get the list of fields returned as a FieldInfo array. So, if you wanted a list of all public or static fields (as an example), you could write something like this (this is a generic method to do this):
public static class MetadataInfo
{
public static FieldInfo[] GetFields<T>()
{
return typeof(T).GetFields(BindingFlags.Public | BindingFlags.Static);
}
}
|
|
|
|
|
I used this part of code, but it is not work for:
int[] x and for internal class B b (see my example).
Now I use the following:
string output = JsonConvert.SerializeObject(a); // Newtonsoft.json
But I want to use C# reflection.
|
|
|
|
|
Works for me.
public static void Main()
{
A a = new A();
FieldInfo[] fi = MetadataInfo.GetFields<A>();
int[] x = (int[])fi[0].GetValue(a);
Console.WriteLine(x[0]);
}
The knack is the cast; if you know what type to expect, then you can simply cast. If you don't know the type, then you have to get the fields of that object and inspect those. May require recursion if you have a lot of nested unknown objects.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
|
Thank you, it works fine!
|
|
|
|
|
Hi,
I would like to show radio buttons of multiple choices in property grid, is there any way i can do that?
|
|
|
|
|
Maybe, but I don't know how to do that. Using a dropdown for multiple-choice is relatively easy though, maybe that's OK as an alternative?
|
|
|
|
|
Yes. Anything you can host on a form, can be shown inside a dropdown editor.
PropertyGrid and Drop Down properties[^]
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
In a simple web application I have:
TryUserName = Environment.GetEnvironmentVariable("UserName");
LogonServer = Environment.GetEnvironmentVariable("LogonServer");
YourDomainName = Environment.GetEnvironmentVariable("UserDomainName");
During development I have a VPN open to the server which is called NZ-SP-AP1.
On my home machine set gives me:-
Username = Ormond
No LogonServer or UserDomainName.
On the server, logged in as Administrator, set gives me:-
Username = Administrator
LogonServer = \NZ-SP-DC1 (correct)
No UserDomainName.
When I open the web page on the server with the variables in a textbox, I get:-
TryUserName = NZ-SP-AP1$
LogonServer is blank.
UserDomainName is blank.
Does anyone know where it gets these values, particularly the Username?
|
|
|
|
|
|
Anticipating your next question: no, there is no way for a website to obtain these details from a user's computer. If there were, that would be a security vulnerability.
If this is an intranet site, and your users are all part of the same domain/forest as the web server, you might be able to use Windows authentication on your site. Depending on their browser settings, they would either automatically log in as themselves, or have to enter their Windows username and password to log in.
Windows Authentication <windowsAuthentication> | Microsoft Docs[^]
If this is an internet site, where your users are not part of your domain, then you cannot obtain any information about their user account.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|