|
I've checked my code, and the url for the image is correct. When I execute GetResponse for the request, a System.Net.WebException is thrown with the message {"The remote server returned an error: (400) Bad Request." } Do you know how I would get the UserAgent of the WebBrowser control?
Thank you,
|
|
|
|
|
You don't - nor can you (at least not in its entirety) - set the WebBrowser's user-agent. You set it on the HttpWebRequest . See the HttpWebBrowser.UserAgent property in the .NET Framework SDK documentation. You could programmatically set this to be the same as the WebBrowser control's with something like this:
IHTMLWindow2 window = (IHTMLWindow2)axWebBrowser;
if (window != null)
{
httpRequest.UserAgent = window.navigator.userAgent;
} That still may not solve the problem. You need to figure out why the server is returning HTTP error code 400. Setting the user-agent may help, but there's many other things that could be wrong.
If you have access to the server, check the lots. If it's ASP.NET, open trace.axd in your browser off the web application's root (like http://localhost/myapp/trace.axd). Tracing has to be turned on for that to work, though, and has to allow connections from your host. All that information is in the .NET Framework SDK under the ASP.NET configuration documentation.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles]
|
|
|
|
|
Hmm, when I try to do that cast,(IHTMLWindow2)axWebBrowser;,
it throws an invalid cast exception. Is there a different way to cast it?
Thanks
|
|
|
|
|
Then the WebBrowser control doesn't implement the IHTMLWindow2 interface (from a COM perspective - not from .NET*). My mistake.
What you'll have to do then is cast WebBrowser.Document to an IHTMLDocument2 (defined in Microsoft.mshtml.dll) then use IHTMLDocument2.parentWindow and cast that to an IHTMLWindow2 . That will get you the reference you need (don't forget to check for null !) to navigator.userAgent , just like you would in script in DHTML.
* When the ComImportAttribute is present on a type, the CLR does a QueryInterface for an interface on an object instead of a cast as performed in IL instructions.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles]
|
|
|
|
|
Interesting, I've definitely learned a lot. I was able to make the cast and pass in the user agent. However, it still sends the 400 bad request exception. What I found though, which is strange is that I can navigate to the image using the webbrowser control using the same or even a different Web Browser Control.
Once the browser has navigated I can call this getimage() function, which grabs the picture indirectly from the graphics card buffer. The downside to this is that the browser has to be in focus of course. I still don't get why it would send a bad request to the HttpWebRequest and not to a browser object, but I'm still glad that I can at least grab the picture indirectly.
Thanks,
<code>
private void getimage()
{
Control c = this.axWebBrowser1 as Control;
Bitmap bmp = CaptureControl(c);
bmp.Save("codeword.jpeg",ImageFormat.Jpeg);
}
[System.Runtime.InteropServices.DllImport("gdi32.dll")] private static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, System.Int32 dwRop);
public static Bitmap CaptureControl(System.Windows.Forms.Control control)
{
Graphics g1 = control.CreateGraphics();
Bitmap bitmap = new Bitmap(control.ClientRectangle.Width, control.ClientRectangle.Height, g1);
Graphics g2 = Graphics.FromImage(bitmap);
IntPtr dc1 = g1.GetHdc();
try
{
IntPtr dc2 = g2.GetHdc();
try
{
BitBlt(dc2, 0, 0, control.ClientRectangle.Width, control.ClientRectangle.Height, dc1, 0, 0, 13369376);
}
finally
{
g2.ReleaseHdc(dc2);
}
}
finally
{
g1.ReleaseHdc(dc1);
}
return bitmap;
}</code>
|
|
|
|
|
You have a major error in your code - though it's a good workaround to solving your problem: anything that implements IDisposable - including (and especially!) Graphics and Bitmap - should be disposed when you're finished. A good way in C# is like so:
using (Graphics g1 = control.CreateGraphics())
{
} The using block makes sure that Dispose is called, even in case of error. It amounts to this:
Graphics g1 = control.CreateGraphics();
try
{
}
finally
{
if (g1 != null) g1.Dispose();
} Actually, the object is always cast to IDisposable in the finally block so that explicit interface implementations are handled correctly, but I didn't want to confuse you.
Other than that, this should work. If you don't dispose your Graphics and Bitmaps above, you'll be leaking resources (memory). Note that you don't have to dispose of controls, though. All Control derivatives encapsulates window handles (HWND s) that are automatically destroyed. Disposing them is not necessary.
In order to see why you're getting HTTP 400, you need to take a look at the HTTP request and response. A simple packet sniffer will help. Make sure that you're also requesting https://... A poorly implemented HTTP daemon expecting an SSL socket connection may return 400. There's many other reasons why this might be failing, however. If you're POSTing data from a web browser, for example, you need to do it again (but can be dangerous! you don't want to order a $4,500 segway twice now, do you?!).
Like I said, the source of your error judging by your description is impossible to determine. You just have to get down and dirty and debug your code, including the HTTP request and responses sent from both your web browser (i.e., the WebBrowser control that you're embedding) as well as your HttpWebRequest and HttpWebResponse .
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles]
|
|
|
|
|
Q. How do you start 2 or more other classes to threading from a single Form1??
I tried this code on Form1 (Form1_Load) but if failed...
objClass1 = new Class1();
objClass1 = new Thread(new ThreadStart(StartMoveKing));
// also
objClass2 = new Class2();
objClass2 = Thread(new ThreadStart(StartMoveQueen));
|
|
|
|
|
Thats because you have to start them, you just constructed them.
objClass1 = new Class1();<br />
objClass1 = new Thread(new ThreadStart(StartMoveKing));<br />
objClass1.Start();<br />
<br />
objClass2 = new Class2();<br />
objClass2 = new Thread(new ThreadStart(StartMoveQueen));<br />
objClass2.Start();
|
|
|
|
|
You have to be careful of what you are doing though.
If those threads have the end purpose of sending new information to a Windows Form you will have very unpredictable results.
You either have to ensure that your threads are within the message pump apartment or you have to alter memory in your program and then setup another thread to monitor changes. The Windows.Forms.Timer will do that. This is a rough example of a skeleton of a program that would accomplish something like that.
Example:
public class Chess()
{
...
public void MoveKing(chessBoard);
{
do {
Stack[] moveStacks = new Stack[10];
....
if (moveMade)
{
move = "K-KR3";
this.Threading.Thread.Stop();
}
...
} while (threadExecutionRequired);
move = "RESIGN";
}
public Chess()
{
IntializeComponents();
threadExecutionRequired=false;
}
public void PlayChess()
{
....
}
private void MakeMyMove()
{
Thread moveThread = new Thread(MoveKing);
moveThread.Start();
this.formTimer.Start();
}
private formTimer_Tick(...)
{
if (ICantWaitAnyLonger)
{
threadExecutionRequired=false;
}
if (move != null)
{
MakeMyMove(move);
DisplayBoard(chessBoard);
formTimer.Stop();
}
}
}
This signature left intentionally blank
|
|
|
|
|
Hi buddies
I am looking for someone who is familiar with iTextSharp library for creating PDF files. I just started with iTextSharp and I have a few basic questions, maybe we could exchage a few emails and it would save me endless digging on the Internet.
If you have some experience with iTextSharp and you would like to help me please reply to this post or write me to HonzaKotera@seznam.cz.
Thanks a lot, H.
|
|
|
|
|
What's your definition of "endless"? It's a SourceForge project, and they all have forums. On the project homepage, click on the first link which takes you to the SourceForce project site. Click on "Forums" in the toolbar. Here's the URL: http://sourceforge.net/forum/?group_id=72954[^].
It seems to be a pretty active forum and you'll find product-specific help there. This forum is for general C# questions and isn't appropriate for product-specific queries. Besides, you'll most likely get better help since you're chances of finding someone that has used iTextSharp here are much, much less than on the iTextSharp forum; there everyone has used it.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles]
|
|
|
|
|
Hey, this is a C# forum, it's not appropriate to ask philosophical questions like "what's endless" or "where's nowhere", check out www.philosophypages.com.
If you are familiar with iTextSharp, probably use it yourself, why can't you just say: "Yes, I know it, how can I help you?" You'd save time to both of us.
I'll try the SourceFroge forum anyway, thanks, H.
|
|
|
|
|
Food for thought:
I would like to know EVERY directory/file entry on my hard drive.
I know that I can write a prog that traverses the entire tree, but I was reading that all information concerning directories/files, i.e., date (created, modified, last accessed), attributes, etc. are contained in meta files (MFT).
Has anyone looked at this. I think that reading the MFT would be a faster solution than traversing the tree.
Michael
If we knew what it was we were doing, it would not be called research, would it? --Albert Einstein
|
|
|
|
|
|
I am a newbie to C# so bear with me
I need some help... have perused the C# URL's ... the more code I have read , the confuser I became
I have a form(named form2) and this has been passed a directory path
(c:\images\)
What I want to do is :
1- reading all of the images in this directory
upon reading an image, shirnk it to a thumbnail
2- populate a listview which contains the columns of icon,path,size,date
icon would be the thumbnail
path.. the path
size ... the size in bytes (or if easier KB, MB)
date is create_date of the file
add row to listview
continue until all files in directory path have finsihed
the listview column structre is NOT set in stone icon+ file_name.ext could be substituted
Hope to hear from you soon
|
|
|
|
|
|
Anyone know when Archer's Inside C# 3rd edition is slated for publishing?
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
|
Hi All,
Is possible a .Net Application run in a Linux Server?
Regards,
Vadim
|
|
|
|
|
|
yes, www.mono-project.com
Judah Himango
|
|
|
|
|
leppie and Judah pointed out a couple alternatives, but make sure you read about them thoroughly.
Both are implementations of the CLI (Common Language Infrastructure), but do not expose the same BCL (base class library) as .NET - Microsoft's implementation of hte CLI. Windows Forms is a big one that is different. In the Microsoft .NET BCL Windows Forms controls (among many other things) P/Invoke native APIs and handle native window messages. That changes with Mono, for example, unless you're using the WINE bindings (as opposed to GTK+ or QT bindings which don't use a message-passing system).
There's other differences as well. While basic code will port to *nix with little to no problems, much code will not.
Take a lot of the .NET controls posted here on CodeProject. Most interop back to the Common Controls their base class encapsupates (like the ListView encapsulates the List-View common control) and use P/Invoke along with handling messages by overriding WndProc . Those wouldn't work on most flavors of Mono on *nix because the subsystem is completely different.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles]
|
|
|
|
|
|
The new managed implementation of Windows.Forms is moving along (internally it uses Win32 messages and codes to help applications that hook up to Wndproc or use CreateParams), the hack-a-thon in Provo, Utah seems to have been a success. I'm not correcting you, merely saying that this statement doesn't really say much at all. They don't talk about what bindings for Windows Forms they're talking about. Keep in mind that with *nix you have many desktop and window managers (both different things). Definitely doesn't detract from the confusion!
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles]
|
|
|
|
|
Heath Stewart wrote:
but do not expose the same BCL (base class library) as .NET
I have to differ here with you. DOtGNU (Portable .NET) aims to provide a 100% BCL as per the ECMA spec which doesnt make any mention of SWF, iow its just mscorlib.dll as we know it. I dont know about mono, as I have seen some funny extra stuff there, but Portable .NET doesnt add anything extra to the BCL as described in the EMCA spec.
Cheers
top secret xacc-ide 0.0.1
|
|
|
|
|