I'm working a project that needs to download a certain webpage content, my code in C# works perfectly, however, it seems that Python can't get connected to the internet throwing an exception.
Perhaps it's because I'm working in a Corporate network and that's why Python might be blocked by Firewall. But, how about a C# code, why it works?
I'd like to emulate the Python code as C# WebClient behavior.
Thanks.
C# Code
WebClient wc = new WebClient();
string str = wc.DownloadString("https://www.google.com");
Console.WriteLine(str);
Pythton Code
import urllib
link = "https://www.google.com"
f = urllib.urlopen(link)
myfile = f.read()
print myfile
Python Exception
[Errno socket error] [Errno 10060] 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
[Edit 01]
[Edit 02]
Tried WebClient.Proxy = null
WebClient wc = new WebClient();
wc.Proxy = null;
string str = wc.DownloadString("https://www.google.com");
Console.WriteLine(str);
Then I got this Exception. (I also got this exception in a Java Program)
{"The operation has timed out"}
at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
at System.Net.WebClient.DownloadString(Uri address)
at System.Net.WebClient.DownloadString(String address)
at WebClientTest.Program.Main(String[] args) in <ProjectPath>\WebClientTest\Program.cs:line 17
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Related
I have a solution that utilises Grapevine to start a server within the Program class.
For some reason though, as soon as I start the program, it crashes when server.Start() is called.
No other parts of the solution are running at this point, so I don't know how that could be happening?
Running in Visual Studio 15, as Administrator, on Windows 10.
Program.cs
using System;
using System.Threading;
using Grapevine.Server;
using QuantConnect.Logging;
namespace QuantConnect.Services
{
class Program
{
/// <summary>
/// QuantConnect C# Services Server:
/// </summary>
static void Main(string[] args)
{
var server = new RESTServer
{
//Host = Configuration.Config.Get("grapevine-host"),
//Port = Configuration.Config.Get("grapevine-port")
Host = Configuration.Config.Get("grapevine-host", "localhost"),
Port = Configuration.Config.Get("grapevine-port", "80")
};
Log.Trace(string.Format("Starting Server On {0}:{1}", server.Host, server.Port));
server.Start(); // Exception occurs here
while (server.IsListening)
{
Thread.Sleep(300);
}
Log.Trace("Server stopped. Press key to continue...");
Console.ReadLine();
}
}
}
Error and stacktrace:
An unhandled exception of type 'System.Net.HttpListenerException' occurred in Grapevine.dll
System.Net.HttpListenerException was unhandled
ErrorCode=32
HResult=-2147467259
Message=The process cannot access the file because it is being used by another process
NativeErrorCode=32
Source=System
StackTrace:
at System.Net.HttpListener.AddAllPrefixes()
at System.Net.HttpListener.Start()
at Grapevine.Server.RESTServer.Start()
at QuantConnect.Services.Program.Main(String[] args) in C:\Users\RichardsPC\Source\Repos\Lean\Services\Program.cs:line 29
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
So I figured out what the issue was.
I set Grapevine to use port 80.
Port = Configuration.Config.Get("grapevine-port", "80")
Issue was that there was another service on my machine using this port, I changed it to use 8080 and it works fine now.
Port = Configuration.Config.Get("grapevine-port", "8080")
I am trying to create a eventhandler for a msmq message router and I almost just copied a online example from msdn library
example 2.
But when I invoke messageQueue.BeginReceive() I get a IllegalFormatName exception.
I am using a private queue
Using VS 2015 on windows 10
Solution is using .Net 4.5.2
I got no problems sending messages to any of my private queues or reading from them if I use messageQueue.Receive()
Exception details below:
System.Messaging.MessageQueueException was unhandled
ErrorCode=-2147467259 HResult=-2147467259 Message=Formatnavnet er
ugyldigt. Source=System.Messaging StackTrace:
ved System.Messaging.MessageQueue.MQCacheableInfo.get_ReadHandle()
ved System.Messaging.MessageQueue.ReceiveAsync(TimeSpan timeout, CursorHandle cursorHandle, Int32 action, AsyncCallback
callback, Object stateObject)
ved System.Messaging.MessageQueue.BeginReceive()
ved MYFirstMSMQ.Program.Main(String[] args) in ......Visual Studio 2015\Projects\FirstQueue_bluff_city\ConsoleApplication1\Program.cs:linje
68
ved System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
ved System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
ved Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
ved System.Threading.ThreadHelper.ThreadStart_Context(Object state)
ved System.Threading.ExecutionContext.RunInternal(ExecutionContext
executionContext, ContextCallback callback, Object state, Boolean
preserveSyncCtx)
ved System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean
preserveSyncCtx)
ved System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
ved System.Threading.ThreadHelper.ThreadStart() InnerException:
Sorry about the Message - windows returns some messages in my native language
I've been having problems with I code I wrote. What this code does is download a picture from my server and puts it inside a PDF document. I've narrowed it down to a problem with the image being downloaded.
At first, I thought it was a problem with my project so I've created a new Console Application with just that code to test it out - but that happens on it as well.
What I'm trying to do is simple - download a picture with WebClient and save it to the disk:
WebClient oWebClient = new WebClient();
byte[] oImageBytes = oWebClient.DownloadData("http://images1.ynet.co.il/PicServer3/2013/12/25/5058381/505838001000100396220.jpg");
ImageConverter oConverter = new ImageConverter();
Image oImg = (Image)oConverter.ConvertFrom(oImageBytes);
oImg.Save("d:\\temp\\1.jpg");
The image url in the example is taken from a local news site and fails as well.
The very strange thing is that on windows 7 and earlier it works. It fails on Windows8 and Windows Server 2012 with the following error:
An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll
Additional information: A generic error occurred in GDI+.
This is the StackTrace:
at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
at System.Drawing.Image.Save(String filename, ImageFormat format)
at System.Drawing.Image.Save(String filename)
at ConsoleApplication1.Program.Main(String[] args) in c:\Users\Ophir\Documents\Visual Studio 2013\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line 23
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
I've tried searching the web, but all solutions I found didn't work in my case.
The directory that I'm trying to save to has sufficient permissions.
Anyone has any idea to why this is happening ?
Thanks
What about to save data as is?
byte[] oImageBytes = oWebClient.DownloadData("http://images1.ynet.co.il/PicServer3/2013/12/25/5058381/505838001000100396220.jpg");
File.WriteAllBytes("d:\\temp\\1.jpg", oImageBytes);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to upload files to Amazon S3 (official SDK) that are larger than 5 MB (approx)?
I try to use the Amazon.S3.IO API. If i write 10mb there is no problem. If i write 21mb i get an exception:
The request was aborted: The request was canceled.
StackTRace:
at System.Net.ConnectStream.CloseInternal(Boolean internalCall, Boolean aborting)
at System.Net.ConnectStream.System.Net.ICloseEx.CloseEx(CloseExState closeState)
at System.Net.ConnectStream.Dispose(Boolean disposing)
at System.IO.Stream.Close()
at System.IO.Stream.Dispose()
at Amazon.S3.AmazonS3Client.getRequestStreamCallback[T](IAsyncResult result)
at Amazon.S3.AmazonS3Client.endOperation[T](IAsyncResult result)
at Amazon.S3.AmazonS3Client.EndPutObject(IAsyncResult asyncResult)
at Amazon.S3.AmazonS3Client.PutObject(PutObjectRequest request)
at Amazon.S3.IO.S3FileStream.Flush(Boolean flushToS3)
at Amazon.S3.IO.S3FileStream.Dispose(Boolean disposing)
at System.IO.Stream.Close()
at System.IO.StreamWriter.Dispose(Boolean disposing)
at System.IO.TextWriter.Dispose()
at S3FileSystem_Sample.Program.createFile(S3DirectoryInfo rootDirectory, String filename) in c:\Program Files (x86)\AWS SDK for .NET\Samples\S3FileSystem_Sample \S3FileSystem_Sample\Program.cs:line 106
at S3FileSystem_Sample.Program.Main(String[] args) in c:\Program Files (x86)\AWS SDK for .NET\Samples\S3FileSystem_Sample\S3FileSystem_Sample\Program.cs:line 59
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
static void createFile(S3DirectoryInfo rootDirectory, string filename)
{
// Creates a file at the root of the bucket.
S3FileInfo readme = rootDirectory.GetFile(filename);
char[] buffer = new char[buffersize];
fillBuffer(buffer);
using (StreamWriter writer = new StreamWriter(readme.OpenWrite()))
{
writer.Write(buffer);
}
}
Any idea i could set the timeout?
Click here and here for more related questions.
Unfortunately the Amazon.S3.IO API does not support setting a timeout but as a Developer on the SDK I will add that feature request to our backlog.
Keep in mind the Amazon.S3.IO API is a really easy interface to work with S3 but it is not well suited for large files because it buffers them into memory until you have passed the entire contents of the file to the API and then it writes it to S3. For large files you should use the TransferUtility found in the the Amazon.S3.Transfer namespace.
I am having the following code to upload a file in s3.
AmazonS3 s3Client = AWSClientFactory.CreateAmazonS3Client("AWSAccessKey", "AWSSecretKey");
String strS3KEY = "JSON_FILE_NAME";
PutObjectRequest request = new PutObjectRequest();
request.WithBucketName("AWS_BUCKET");
request.WithKey(strS3KEY);
request.WithContentBody("Test");
request.CannedACL = S3CannedACL.PublicRead;
s3Client.PutObject(request);
This works find in the local system. When I run the application from deployment server in the line s3Client.PutObject(request); it gives the following error-
The remote server returned an error: (407) Proxy Authentication Required.
The stack trace is -
at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
at System.Net.HttpWebRequest.GetRequestStream()
at Amazon.S3.AmazonS3Client.getRequestStreamCallback[T](IAsyncResult result)
at Amazon.S3.AmazonS3Client.endOperation[T](IAsyncResult result)
at Amazon.S3.AmazonS3Client.EndPutObject(IAsyncResult asyncResult)
at Amazon.S3.AmazonS3Client.PutObject(PutObjectRequest request)
at XMLandCloudConsole.Program.Main(String[] args) in C:\Users\spxc\Desktop\consoletest \XMLandCloudConsole\XMLandCloudConsole\Program.cs:line 29
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()