I have the following code which tries to upload the picture to picasa website. when I m trying to upload I m getting Unauthorised access exception. I dont know how to get the AuthToken.
Here is my code . Please let me know if you have any clues.
public delegate void UploadPhotoCallback(bool success, string message);
public static void UploadPhoto(string albumId, string originalFileName, byte[] photo, UploadPhotoCallback callback)
{
string Username = "mailmugu";
string AuthToken = "";
try
{
var url = string.Format("http://picasaweb.google.com/data/feed/api/user/{0}/albumid/{1}", Username, albumId);
var request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
//request.ContentType = HttpFormPost.ImageJpeg;
//request.Method = HttpMethods.Post;
request.ContentType = "image/jpeg";
request.Method = "POST";
request.Headers[HttpRequestHeader.Authorization] = "GoogleLogin auth=" + AuthToken;
request.BeginGetRequestStream(new AsyncCallback(UploadGetRequestCallback),
new UploadRequestState
{
Request = request,
Callback = callback,
Photo = photo,
FileName = originalFileName
});
}
catch (Exception e)
{
Console.WriteLine(e);
//throw new MyException(MyResources.ErrorUploadingPhotoMessage, e);
}
}
private static void UploadGetRequestCallback(IAsyncResult ar)
{
try
{
var state = (UploadRequestState)ar.AsyncState;
HttpWebRequest request = state.Request;
// End the operation
var stream = request.EndGetRequestStream(ar);
stream.Write(state.Photo, 0, state.Photo.Length);
stream.Close();
request.BeginGetResponse(UploadGetResponseCallback, state);
}
catch (Exception e)
{
//throw new MyException(MyResources.ErrorUploadingPhotoMessage, e);
}
}
private static void UploadGetResponseCallback(IAsyncResult ar)
{
UploadRequestState state = null;
try
{
state = (UploadRequestState)ar.AsyncState;
HttpWebRequest request = state.Request;
// End the operation
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(ar);
if (response != null)
{
response.Close();
}
if (state.Callback != null)
{
MessageBox.Show("Uploaded Sucessfully");
//state.Callback(true, MyResources.PhotosUploadedMessage);
}
}
catch (Exception e)
{
MessageBox.Show("Error" + e.Message);
Console.Write(e);
//if (state != null && state.Callback != null)
//state.Callback(false, MyResources.ErrorSavingImageMessage);
}
}
public class UploadRequestState
{
public HttpWebRequest Request { get; set; }
public UploadPhotoCallback Callback { get; set; }
public byte[] Photo { get; set; }
public string FileName { get; set; }
}
private void button1_Click(object sender, RoutedEventArgs e)
{
string albumId = "Picasa";
string Filename = "Test";
UploadRequestState _uploadReq = new UploadRequestState();
Uri myuri = new Uri("/Images/Test.jpg", UriKind.RelativeOrAbsolute);
BitmapImage btmMap = new BitmapImage(myuri);
image1.Source=btmMap;
WriteableBitmap bmp = new WriteableBitmap(btmMap.PixelWidth,btmMap.PixelHeight);
MemoryStream ms = new MemoryStream();
// write an image into the stream
Extensions.SaveJpeg(bmp, ms,
btmMap.PixelWidth, btmMap.PixelHeight, 0, 100);
byte[] Photo = ms.ToArray();
UploadPhoto(albumId,Filename,Photo,_uploadReq.Callback);
}
}
}
Since your code does not even attempt to get the Authentication Token required to do what you want I suggest looking at http://code.google.com/apis/picasaweb/docs/2.0/developers_guide_protocol.html#Auth and open a new question to address any concerns you might have.
Related
I am trying to open pdf in the cefsharp browser itself. But nothing is getting displayed when I open the pdf. I have set the logs to error level but there are no errors in it.
This is how I am registering the CefCustomScheme :
settings.RegisterScheme(new CefCustomScheme
{
SchemeName = CefSharpSchemeHandlerFactory.SchemeName,
SchemeHandlerFactory = new CefSharpSchemeHandlerFactory(),
IsCSPBypassing = true,
});
This is my CefSharpSchemeHandlerFactory :
public class CefSharpSchemeHandlerFactory : ISchemeHandlerFactory
{
public const string SchemeName = "local";
public IResourceHandler Create(IBrowser browser, IFrame frame, string schemeName, IRequest request)
{
if (schemeName == SchemeName && request.Url.EndsWith("CefSharp.Core.xml", System.StringComparison.OrdinalIgnoreCase))
{
//Convenient helper method to lookup the mimeType
var mimeType = ResourceHandler.GetMimeType(".xml");
//Load a resource handler for CefSharp.Core.xml
//mimeType is optional and will default to text/html
return ResourceHandler.FromFilePath("CefSharp.Core.xml", mimeType);
}
return new CefSharpSchemeHandler();
}
}
This is my CefSharpSchemeHandler :
internal class CefSharpSchemeHandler : IResourceHandler
{
private static readonly IDictionary<string, string> ResourceDictionary;
private string mimeType;
private MemoryStream stream;
static CefSharpSchemeHandler()
{
}
bool IResourceHandler.ProcessRequest(IRequest request, ICallback callback)
{
// The 'host' portion is entirely ignored by this scheme handler.
var uri = new Uri(request.Url);
var fileName = uri.AbsolutePath;
string resource;
if (ResourceDictionary.TryGetValue(fileName, out resource) && !string.IsNullOrEmpty(resource))
{
Task.Run(() =>
{
using (callback)
{
var bytes = Encoding.UTF8.GetBytes(resource);
stream = new MemoryStream(bytes);
var fileExtension = Path.GetExtension(fileName);
mimeType = ResourceHandler.GetMimeType(fileExtension);
callback.Continue();
}
});
return true;
}
else
{
callback.Dispose();
}
return false;
}
void IResourceHandler.GetResponseHeaders(IResponse response, out long responseLength, out string redirectUrl)
{
responseLength = stream == null ? 0 : stream.Length;
redirectUrl = null;
response.StatusCode = (int)HttpStatusCode.OK;
response.StatusText = "OK";
response.MimeType = mimeType;
}
bool IResourceHandler.ReadResponse(Stream dataOut, out int bytesRead, ICallback callback)
{
callback.Dispose();
if(stream == null)
{
bytesRead = 0;
return false;
}
//Data out represents an underlying buffer (typically 32kb in size).
var buffer = new byte[dataOut.Length];
bytesRead = stream.Read(buffer, 0, buffer.Length);
dataOut.Write(buffer, 0, buffer.Length);
return bytesRead > 0;
}
bool IResourceHandler.CanGetCookie(Cookie cookie)
{
return true;
}
bool IResourceHandler.CanSetCookie(Cookie cookie)
{
return true;
}
void IResourceHandler.Cancel()
{
}
void IDisposable.Dispose()
{
}
}
And this how I am opening My pdf file from some other class :
string pdfPath = #"D:\Magic\10774.pdf";
var mimeType = ResourceHandler.GetMimeType(".pdf");
ResourceHandler.FromFilePath(pdfPath, mimeType);
I am not getting a hit at CefSharpSchemeHandlerFactory create method.
I'm uploading excel file from android to C# the request from android is arriving to c# server(I don't want to use asp.net).
I've built from scratch a simple HTTPServe, I do't how to handle multipart data upload in c# that's my code :
Request.cs:
class Request
{
public String type { get; set; }
public String url { get; set; }
public String host { get; set; }
private Request(String type,String url,String host)
{
this.type = type;
this.url = url;
this.host = host;
}
public static Request GetRequest(String request)
{
if (String.IsNullOrEmpty(request))
return null;
String[] tokens = request.Split(' ');
String type = tokens[0];
String url = tokens[1];
String host = tokens[4];
return new Request(type, url, host);
}
}
Response.cs:
class Response
{
private Byte[] data = null;
private String status;
private String mime;
private Response(String status,String mime,Byte[] data)
{
this.status = status;
this.data = data;
this.mime = mime;
}
public static Response From(Request request)
{
Console.WriteLine(request.type);
if (request == null)
return MakeNullRequest();
if (request.type.Equals("POST"))
{
return UploadCompleteResponse(request);
}
return MakeFromExcel();
}
private static Response UploadCompleteResponse(Request request)
{ //Handling the multipart request here but I don't know how that is a simple example
Console.WriteLine("uploaded!!");
byte[] bytes = Encoding.ASCII.GetBytes("uploaded!!");
return new Response("ok 200","text/plain",bytes);
}
//private static Response MakeFromFile(FileInfo f)
//{
// // to do later
// //return new Response("200 ok", "text/html", d);
//}
//Retruning an excel file response
private static Response MakeFromExcel()
{
String file = Environment.CurrentDirectory + HTTPServer.EXCEL_DIR+"brains.xlsx";
Console.WriteLine(file);
FileInfo info = new FileInfo(file);
if (info.Exists)
{
FileStream fs = info.OpenRead();
BinaryReader read = new BinaryReader(fs);
Byte[] d = new Byte[fs.Length];
read.Read(d, 0, d.Length);
fs.Close();
return new Response("200 ok", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", d);
}
return PageNotFound();
}
private static Response MakeNullRequest()
{
String file = Environment.CurrentDirectory+HTTPServer.MSG_DIR +"400.html";
Console.WriteLine(file);
FileInfo info = new FileInfo(file);
FileStream fs=info.OpenRead();
BinaryReader read=new BinaryReader(fs);
Byte[] d = new Byte[fs.Length];
read.Read(d, 0, d.Length);
fs.Close();
return new Response("400 Bad Request", "text/html", d);
}
private static Response PageNotFound()
{
String file = Environment.CurrentDirectory + HTTPServer.MSG_DIR + "400.html";
FileInfo info = new FileInfo(file);
FileStream fs = info.OpenRead();
BinaryReader read = new BinaryReader(fs);
Byte[] d = new Byte[fs.Length];
read.Read(d, 0, d.Length);
return new Response("404 Not Found", "text/html", d);
}
public void Post(NetworkStream stream)
{
StreamWriter writer = new StreamWriter(stream);
writer.WriteLine(String.Format("{0} {1}\r\nServer: {2}\r\nContent-Type: {3}\r\nAccept-Ranges: 255 bytes\r\nContent-Length: {4}\r\n", HTTPServer.VERSION
, status, HTTPServer.NAME, mime, data.Length));
writer.Flush();
stream.Write(data, 0, data.Length);
stream.Flush();
stream.Close();
}
}
In the above code I used more than one type of response You can access and the request fields by increasing the size of the token array.
HTTServer.cs:
class HTTPServer
{
public const String MSG_DIR = "\\root\\msg\\";
public const String EXCEL_DIR = "\\root\\excel\\";
public const String WEB_DIR = "\\root\\web\\";
public const String VERSION = "HTTP/1.1";
public const String NAME = "Thecode007 HTTP SERVER v 1.0";
private bool running = false;
private TcpListener listener;
public HTTPServer(int port)
{
listener = new TcpListener(IPAddress.Any,port);
}
public void Start() {
Thread serverThread = new Thread(new ThreadStart(Run));
serverThread.Start();
}
private void Run()
{
running = true;
listener.Start();
while (running)
{
Console.WriteLine("Waiting for connection...");
TcpClient client = listener.AcceptTcpClient();
HandleClient(client);
Console.WriteLine("Client connected!");
}
running = false;
listener.Stop();
}
private void HandleClient(TcpClient client)
{
StreamReader reader = new StreamReader(client.GetStream());
String msg = "";
while (reader.Peek() != -1)
{
msg += reader.ReadLine()+"\n";
}
Console.WriteLine(msg);
Request req = Request.GetRequest(msg);
Response resp = Response.From(req);
resp.Post(client.GetStream());
}
}
And here is the main method:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Starting Serving on port 9000");
HTTPServer server = new HTTPServer(9000);
server.Start();
Console.ReadLine();
}
}
Disclaimer: You should never write a HTTP server directly with TcpClient for any production work. There are far too many specifications to be accounted for and C# has these built in to any Asp.Net web server you use.
That being said, for an exercise this is a matter of parsing the request in a specific way. You should be able to reference these questions to learn more about the details of multipart boundary:
What is http multipart request?
What should a Multipart HTTP request with multiple files look like?
Essentially, you will have to read a header from the http headers (Content-Type), and parse the value of boundary= to determine what is separating the key/value pairs of the multipart form in the request body. Once you've got the boundary you will have to parse the body of the request into the real key/value pairs of the form. From there, the contents of the uploaded file will be the value of whatever key the form submits it as.
Read more on the official W3 spec
Response.cs:
class Response
{
private Byte[] data = null;
private String status;
private String mime;
private Response(String status,String mime,Byte[] data)
{
this.status = status;
this.data = data;
this.mime = mime;
}
public static Response From(Request request)
{
if (request == null)
return MakeNullRequest();
if (request.type.Equals("POST"))
{
return UploadCompleteResponse(request);
}
return MakeFromExcel();
}
private static Response UploadCompleteResponse(Request request)
{ //I added on the rquest array the datapart which contains data
String data=request.data;
byte[] bytes = Encoding.ASCII.GetBytes("uploaded!!");
return new Response("ok 200","text/plain",bytes);
}
//private static Response MakeFromFile(FileInfo f)
//{
// // to do later
// //return new Response("200 ok", "text/html", d);
//}
//Retruning an excel file response
private static Response MakeFromExcel()
{
String file = Environment.CurrentDirectory + HTTPServer.EXCEL_DIR+"brains.xlsx";
Console.WriteLine(file);
FileInfo info = new FileInfo(file);
if (info.Exists)
{
FileStream fs = info.OpenRead();
BinaryReader read = new BinaryReader(fs);
Byte[] d = new Byte[fs.Length];
read.Read(d, 0, d.Length);
fs.Close();
return new Response("200 ok", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", d);
}
return PageNotFound();
}
private static Response MakeNullRequest()
{
String file = Environment.CurrentDirectory+HTTPServer.MSG_DIR +"400.html";
Console.WriteLine(file);
FileInfo info = new FileInfo(file);
FileStream fs=info.OpenRead();
BinaryReader read=new BinaryReader(fs);
Byte[] d = new Byte[fs.Length];
read.Read(d, 0, d.Length);
fs.Close();
return new Response("400 Bad Request", "text/html", d);
}
private static Response PageNotFound()
{
String file = Environment.CurrentDirectory + HTTPServer.MSG_DIR + "400.html";
FileInfo info = new FileInfo(file);
FileStream fs = info.OpenRead();
BinaryReader read = new BinaryReader(fs);
Byte[] d = new Byte[fs.Length];
read.Read(d, 0, d.Length);
return new Response("404 Not Found", "text/html", d);
}
public void Post(NetworkStream stream)
{
try
{
StreamWriter writer = new StreamWriter(stream);
writer.WriteLine(String.Format("{0} {1}\r\nServer: {2}\r\nContent-Type: {3}\r\nAccept-Ranges: 255 bytes\r\nContent-Length: {4}\r\n", HTTPServer.VERSION
, status, HTTPServer.NAME, mime, data.Length));
writer.Flush();
stream.Write(data, 0, data.Length);
stream.Flush();
stream.Close();
}catch(Exception ex){
}
}
}
Request.cs:
class Request
{
public String type { get; set; }
public String url { get; set; }
public String host { get; set; }
public String data { get; set; }
private Request(String type,String url,String host)
{
this.type = type;
this.url = url;
this.host = host;
}
private Request(String type, String url, String host,String data)
{
this.type = type;
this.url = url;
this.host = host;
this.data = data;
}
public static Request GetRequest(String request)
{
if (String.IsNullOrEmpty(request))
return null;
String[] tokens = request.Split(' ');
String type = tokens[0];
String url = tokens[1];
String host = tokens[4];
String data = "N/A";
if (tokens.Length >= 9)
{
data = tokens[8];
}
return new Request(type, url, host, data);
}
}
Secondly my android client was excpecting a response which is responsbody type not a plain text thats why the data was never sent....
Iam using CefSharp's SchemeHandler in order to grab resources from my C# project like .css, .js or .png files using a custom url for example custom://cefsharp/assets/css/style.css
I've 2 custom classes in order to archive this.
First class, MyCustomSchemeHandlerFactory will be the one that handles the custom Scheme and it looks like this, where "custom" will be the custom scheme:
internal class MyCustomSchemeHandlerFactory : ISchemeHandlerFactory
{
public const string SchemeName = "custom";
public IResourceHandler Create(IBrowser browser, IFrame frame, string schemeName, IRequest request)
{
return new MyCustomSchemeHandler();
}
}
The next class I've implemented is MyCustomSchemeHandler which will receive the call and output a response and it looks like this:
internal class MyCustomSchemeHandler : IResourceHandler
{
private static readonly IDictionary<string, string> ResourceDictionary;
private string mimeType;
private MemoryStream stream;
static MyCustomSchemeHandler()
{
ResourceDictionary = new Dictionary<string, string>
{
{ "/home.html", Properties.Resources.index},
{ "/assets/css/style.css", Properties.Resources.style}
};
}
public Stream Stream { get; set; }
public int StatusCode { get; set; }
public string StatusText { get; set; }
public string MimeType { get; set; }
public NameValueCollection Headers { get; private set; }
public Stream GetResponse(IResponse response, out long responseLength, out string redirectUrl)
{
redirectUrl = null;
responseLength = -1;
response.MimeType = MimeType;
response.StatusCode = StatusCode;
response.StatusText = StatusText;
response.ResponseHeaders = Headers;
var memoryStream = Stream as MemoryStream;
if (memoryStream != null)
{
responseLength = memoryStream.Length;
}
return Stream;
}
public bool ProcessRequestAsync(IRequest request, ICallback callback)
{
// The 'host' portion is entirely ignored by this scheme handler.
var uri = new Uri(request.Url);
var fileName = uri.AbsolutePath;
string resource;
if (ResourceDictionary.TryGetValue(fileName, out resource) && !string.IsNullOrEmpty(resource))
{
var resourceHandler = ResourceHandler.FromString(resource);
stream = (MemoryStream)resourceHandler.Stream;
var fileExtension = Path.GetExtension(fileName);
mimeType = ResourceHandler.GetMimeType(fileExtension);
callback.Continue();
return true;
}
else
{
callback.Dispose();
}
return false;
}
void GetResponseHeaders(IResponse response, out long responseLength, out string redirectUrl)
{
responseLength = stream == null ? 0 : stream.Length;
redirectUrl = null;
response.StatusCode = (int)HttpStatusCode.OK;
response.StatusText = "OK";
response.MimeType = mimeType;
}
bool ReadResponse(Stream dataOut, out int bytesRead, ICallback callback)
{
//Dispose the callback as it's an unmanaged resource, we don't need it in this case
callback.Dispose();
if (stream == null)
{
bytesRead = 0;
return false;
}
//Data out represents an underlying buffer (typically 32kb in size).
var buffer = new byte[dataOut.Length];
bytesRead = stream.Read(buffer, 0, buffer.Length);
dataOut.Write(buffer, 0, buffer.Length);
return bytesRead > 0;
}
bool CanGetCookie(Cookie cookie)
{
return true;
}
bool CanSetCookie(Cookie cookie)
{
return true;
}
void Cancel()
{
}
}
Inside this class I've defined a custom resource dictionary which will dictate what file from the resources will be used, so as I stated in the first example, custom://cefsharp/assets/css/style.css should load the resource Properties.Resources.style, the problem is that nothing gets loaded once I enter to the specific url, I've tried to output the mimeType and It works but somehow the file itself won't output correctly. Is there something wrong with my implementation?
Additionaly I've tried to output the raw file in the form of:
if (ResourceDictionary.TryGetValue(fileName, out resource) && !string.IsNullOrEmpty(resource))
{
MessageBox.Show(resource);
}
And it outputs the correct file without any problems.
To load the custom Scheme I use the following code before initializing CefSharp:
var settings = new CefSettings();
settings.RegisterScheme(new CefCustomScheme
{
SchemeName = MyCustomSchemeHandlerFactory.SchemeName,
SchemeHandlerFactory = new MyCustomSchemeHandlerFactory()
});
The above classes were based on the following links:
MyCustomSchemeHandlerFactory: FlashResourceHandlerFactory.cs
MyCustomSchemeHandler: CefSharpSchemeHandler.cs and ResourceHandler.cs
Since Cefsharp changed a bit in last few months here is an updated and easier way of handling 'file' protocol. I wrote blog post on this matter.
What you want to add is your scheme handler and its factory:
using System;
using System.IO;
using CefSharp;
namespace MyProject.CustomProtocol
{
public class CustomProtocolSchemeHandler : ResourceHandler
{
// Specifies where you bundled app resides.
// Basically path to your index.html
private string frontendFolderPath;
public CustomProtocolSchemeHandler()
{
frontendFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "./bundle/");
}
// Process request and craft response.
public override bool ProcessRequestAsync(IRequest request, ICallback callback)
{
var uri = new Uri(request.Url);
var fileName = uri.AbsolutePath;
var requestedFilePath = frontendFolderPath + fileName;
if (File.Exists(requestedFilePath))
{
byte[] bytes = File.ReadAllBytes(requestedFilePath);
Stream = new MemoryStream(bytes);
var fileExtension = Path.GetExtension(fileName);
MimeType = GetMimeType(fileExtension);
callback.Continue();
return true;
}
callback.Dispose();
return false;
}
}
public class CustomProtocolSchemeHandlerFactory : ISchemeHandlerFactory
{
public const string SchemeName = "customFileProtocol";
public IResourceHandler Create(IBrowser browser, IFrame frame, string schemeName, IRequest request)
{
return new CustomProtocolSchemeHandler();
}
}
}
And then register it before calling Cef.Initialize:
var settings = new CefSettings
{
BrowserSubprocessPath = GetCefExecutablePath()
};
settings.RegisterScheme(new CefCustomScheme
{
SchemeName = CustomProtocolSchemeHandlerFactory.SchemeName,
SchemeHandlerFactory = new CustomProtocolSchemeHandlerFactory()
});
If you simply need to return a string, then you can use ResourceHandler.FromString(html, mimeType). For this you just need to implement the ISchemeHandlerFactory.
https://github.com/cefsharp/CefSharp/blob/cefsharp/47/CefSharp/ResourceHandler.cs#L98
Example reading from a file https://github.com/cefsharp/CefSharp/blob/cefsharp/47/CefSharp.Example/CefSharpSchemeHandlerFactory.cs#L17 which can be translated to reading from a string quite simply.
I have small program that takes the links from a text file, pass those links to backend system at ImportIO, and save the results to a CSV. However I am seeing following errors after 15,20 min of run. I encounter two exception whichever comes first
1. System.OutOfMemoryException
OR
2. System.NUllReferenceException
Both of these are however I feel my fault somewhere in the code. I am not an expert but I tried to use timer, or closing the files, or even setting objects to null. None worked or even using ArgumentNullException.
I ran the code analysis and it suggested that I should Idispose by this error.
CA1001 Types that own disposable fields should be disposable Implement
IDisposable on 'ImportIO' because it creates members of the following
IDisposable types: 'BlockingCollection>'. Ostock Main.cs 232
My code is as followed, I am not including importIO class it is long. I think solution is easy but I am just not on right path. Could you guys please help?
namespace MinimalCometLibrary
{
class Program
{
private static CountdownEvent countdownLatch;
static void Main(string[] args)
{
string[] lines = File.ReadAllLines(#"C:\Users\James\Desktop\Exper\Input_Links\Stock_links.txt");
for (int i = 0; i < lines.Length; i++)
{
string[] line = lines[i].Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries);
for (int j = 0; j < line.Length; j++)
{
ImportIO io = new ImportIO("https://query.import.io", Guid.Parse("sdasd-asdasd-NoReal-3easdecb"), "NoReal=");
/* Time Starts
Stopwatch sw = new Stopwatch(); // sw cotructor
sw.Start(); // starts the stopwatch
for (int b = 0; ; b++)
{
if (b % 1000 == 0) // if in 100000th iteration (could be any other large number
// depending on how often you want the time to be checked)
{
sw.Stop(); // stop the time measurement
if (sw.ElapsedMilliseconds > 25) // check if desired period of time has elapsed
{
break; // if more than 5000 milliseconds have passed, stop looping and return
// to the existing code
}
else
{
sw.Start(); // if less than 5000 milliseconds have elapsed, continue looping
// and resume time measurement
}
}
}
//Time Ends
*/
io.Connect();
countdownLatch = new CountdownEvent(1);
// Query for tile SamsClub_Extractor, line[j]
Dictionary<String, Object> query1 = new Dictionary<string, object>();
query1.Add("input", new Dictionary<String, String>() { { "webpage/url", line[j] } });
query1.Add("connectorGuids", new List<String>() { "189f34f3-0f82-4abb-8fbc-f353f35a255a" });
io.DoQuery(query1, HandleQuery);
countdownLatch.Wait();
io.Disconnect();
}
}
Environment.Exit(0);
}
private static void HandleQuery(Query query, Dictionary<String, Object> message)
{
if (message["type"].Equals("MESSAGE"))
{
Console.WriteLine("Got data!");
string JSON = JsonConvert.SerializeObject(message["data"]);
//Deserialize to strongly typed class i.e., RootObject
RootObject obj = JsonConvert.DeserializeObject<RootObject>(JSON);
// handle null reference
if (obj == null) { throw new ArgumentNullException("PleaseKeepRunning"); }
//loop through the list and write to CSV file
foreach (Result resultsItem in obj.results)
{
Console.WriteLine(resultsItem.itemnbr + "-" + resultsItem.price +
"-" + resultsItem.product_name + "_" + obj.pageUrl);
string filePath = #"C:\Users\James\Desktop\Exper\Output_Files\StockPrice_NOW.txt";
//checking if file already exists, if not, create it:
if (!File.Exists(filePath))
{
FileStream fs = new FileStream(filePath, FileMode.CreateNew);
fs.Close();
}
//writing to a file (appending text):
using (FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write))
{
using (TextWriter tw = new StreamWriter(fs))
tw.WriteLine(resultsItem.itemnbr + "\t" + resultsItem.price + "\t" + resultsItem.product_name + "\t" + resultsItem.misc +
"\t" + resultsItem.qty + "\t" + obj.pageUrl);
fs.Close();
}
//Set object to null
obj = null;
obj.results = null;
}
}
if (query.isFinished) countdownLatch.Signal();
}
}
//root Object
public class Result
{
public double price { get; set; }
public string itemnbr { get; set; }
public string product_name { get; set; }
public string qty { get; set; }
public string misc { get; set; }
}
public class RootObject
{
public List<string> cookies { get; set; }
public List<Result> results { get; set; }
public string pageUrl { get; set; }
public string connectorGuid { get; set; }
public string connectorVersionGuid { get; set; }
public int offset { get; set; }
}
Please excuse my limited knowledge in .net. I am totally new to it. :)
Thanks
---- Edit
I used dispose and using as suggested but I am still facing the error. I am seeing error exception and debugger highlight this code of line in importIO.
new Thread(new ThreadStart(PollQueue)).Start();
I also observe that stock.vshost.exe *32 also keep increasing memory and throw out of memory exception at any time after 70MB or something. I am including the importIO class code
class ImportIO
{
private String host { get; set; }
private int port { get; set; }
private Guid userGuid;
private String apiKey;
private static String messagingChannel = "/messaging";
private String url;
private int msgId = 0;
private String clientId;
private Boolean isConnected;
CookieContainer cookieContainer = new CookieContainer();
Dictionary<Guid, Query> queries = new Dictionary<Guid, Query>();
private BlockingCollection<Dictionary<String, Object>> messageQueue = new BlockingCollection<Dictionary<string, object>>();
public ImportIO(String host = "http://query.import.io", Guid userGuid = default(Guid), String apiKey = null)
{
this.userGuid = userGuid;
this.apiKey = apiKey;
this.url = host + "/query/comet/";
clientId = null;
}
public void Login(String username, String password, String host = "http://api.import.io")
{
Console.WriteLine("Logging in");
String loginParams = "username=" + HttpUtility.UrlEncode(username) + "&password=" + HttpUtility.UrlEncode(password);
String searchUrl = host + "/auth/login";
HttpWebRequest loginRequest = (HttpWebRequest)WebRequest.Create(searchUrl);
loginRequest.Method = "POST";
loginRequest.ContentType = "application/x-www-form-urlencoded";
loginRequest.ContentLength = loginParams.Length;
loginRequest.CookieContainer = cookieContainer;
using (Stream dataStream = loginRequest.GetRequestStream())
{
dataStream.Write(System.Text.UTF8Encoding.UTF8.GetBytes(loginParams), 0, loginParams.Length);
HttpWebResponse loginResponse = (HttpWebResponse)loginRequest.GetResponse();
if (loginResponse.StatusCode != HttpStatusCode.OK)
{
throw new Exception("Could not log in, code:" + loginResponse.StatusCode);
}
else
{
foreach (Cookie cookie in loginResponse.Cookies)
{
if (cookie.Name.Equals("AUTH"))
{
// Login was successful
Console.WriteLine("Login Successful");
}
}
}
}
}
public List<Dictionary<String, Object>> Request(String channel, Dictionary<String, Object> data = null, String path = "", Boolean doThrow = true)
{
Dictionary<String, Object> dataPacket = new Dictionary<String, Object>();
dataPacket.Add("channel", channel);
dataPacket.Add("connectionType", "long-polling");
dataPacket.Add("id", (msgId++).ToString());
if (this.clientId != null)
dataPacket.Add("clientId", this.clientId);
if (data != null)
{
foreach (KeyValuePair<String, Object> entry in data)
{
dataPacket.Add(entry.Key, entry.Value);
}
}
String url = this.url + path;
if (apiKey != null)
{
url += "?_user=" + HttpUtility.UrlEncode(userGuid.ToString()) + "&_apikey=" + HttpUtility.UrlEncode(apiKey);
}
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.AutomaticDecompression = DecompressionMethods.GZip;
request.Method = "POST";
request.ContentType = "application/json;charset=UTF-8";
request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip");
String dataJson = JsonConvert.SerializeObject(new List<Object>() { dataPacket });
request.ContentLength = dataJson.Length;
request.CookieContainer = cookieContainer;
using (Stream dataStream = request.GetRequestStream())
{
dataStream.Write(System.Text.UTF8Encoding.UTF8.GetBytes(dataJson), 0, dataJson.Length);
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader responseStream = new StreamReader(response.GetResponseStream()))
{
String responseJson = responseStream.ReadToEnd();
List<Dictionary<String, Object>> responseList = JsonConvert.DeserializeObject<List<Dictionary<String, Object>>>(responseJson);
foreach (Dictionary<String, Object> responseDict in responseList)
{
if (responseDict.ContainsKey("successful") && (bool)responseDict["successful"] != true)
{
if (doThrow)
throw new Exception("Unsucessful request");
}
if (!responseDict["channel"].Equals(messagingChannel)) continue;
if (responseDict.ContainsKey("data"))
{
messageQueue.Add(((Newtonsoft.Json.Linq.JObject)responseDict["data"]).ToObject<Dictionary<String, Object>>());
}
}
return responseList;
}
}
catch (Exception e)
{
Console.WriteLine("Error occurred {0}", e.Message);
return new List<Dictionary<String, Object>>();
}
}
}
public void Handshake()
{
Dictionary<String, Object> handshakeData = new Dictionary<String, Object>();
handshakeData.Add("version", "1.0");
handshakeData.Add("minimumVersion", "0.9");
handshakeData.Add("supportedConnectionTypes", new List<String> { "long-polling" });
handshakeData.Add("advice", new Dictionary<String, int>() { { "timeout", 60000 }, { "interval", 0 } });
List<Dictionary<String, Object>> responseList = Request("/meta/handshake", handshakeData, "handshake");
clientId = (String)responseList[0]["clientId"];
}
public void Connect()
{
if (isConnected)
{
return;
}
Handshake();
Dictionary<String, Object> subscribeData = new Dictionary<string, object>();
subscribeData.Add("subscription", messagingChannel);
Request("/meta/subscribe", subscribeData);
isConnected = true;
new Thread(new ThreadStart(Poll)).Start();
new Thread(new ThreadStart(PollQueue)).Start();
}
public void Disconnect()
{
Request("/meta/disconnect", null, "", true);
isConnected = false;
}
private void Poll()
{
while (isConnected)
{
Request("/meta/connect", null, "connect", false);
}
}
private void PollQueue()
{
while (isConnected)
{
ProcessMessage(messageQueue.Take());
}
}
private void ProcessMessage(Dictionary<String, Object> data)
{
Guid requestId = Guid.Parse((String)data["requestId"]);
Query query = queries[requestId];
query.OnMessage(data);
if (query.isFinished)
{
queries.Remove(requestId);
}
}
public void DoQuery(Dictionary<String, Object> query, QueryHandler queryHandler)
{
Guid requestId = Guid.NewGuid();
queries.Add(requestId, new Query(query, queryHandler));
query.Add("requestId", requestId);
Request("/service/query", new Dictionary<String, Object>() { { "data", query } });
}
}
Try calling Dispose() method because as seen in your error message , it's a memory error because you keep opening files and reading them and then keeping the data there loaded on memory which causes the crash you see after some time
Try this :
if (!File.Exists(filePath))
{
FileStream fs = new FileStream(filePath, FileMode.CreateNew);
fs.Close();
fs.Dispose();
}
Also use the using() { } for the ImportIO class
using(ImportIO myIO = new ImportIO) { }
I can't say if your exceptions are related to it or not without seeing complete code, but the warning is about an issue "in" ImportIO, not in the code that's calling it (it's complaining that the ImportIO class does not implement IDisposable, not that you do something wrong with it)
Since you edited with the class : just implement IDisposable on your ImportIO class, here's a link on how to properly implement IDisposable : Implementing IDisposable correctly
Make sure to dispose of the BlockingCollection in the dispose of your ImportIO.
Then wrap your ImportIO in a using and the warning should go away. I'd be surprised if that alone caused those exceptions however but this is the way to fix your warning.
Is there anyway to read the specific line?
http://i.stack.imgur.com/hDPIg.jpg
XDocument dataFeed = XDocument.Parse(e.Result);
AchivementsListBox.ItemsSource = from query in dataFeed.Descendants("MaxPayne3")
select new NewGamesClass
{
GameGuide = (string)query.Element("Guide")
};
I would use a single WebClient instance to download the files, and simply reduce it to a single event handler. More than that, you have a similar document structure from what I can tell. That means that you could also re-use your reading code instead of writing it twice.
Something like this:
void Download(GameType type, Action onCompletion)
{
WebClient client = new WebClient();
client.DownloadProgressChanged += (s, args) =>
{
// Handle change in progress.
};
client.DownloadStringCompleted += (s, args) =>
{
XDocument feed = XDocument.Parse(args.Result);
var itemSource = from query in feed.Root
select new NewGamesClass
{
NewGameTitle = (string)query.Element("Title"),
NewGameDescription = (string)query.Element("Descript
NewGameImage = (string)query.Element("Image")
};
switch (type)
{
case GameType.ComingSoon:
{
ComingSoonList.ItemSource = itemSource;
}
case GameType.NewGames:
{
NewGameList.ItemSource = itemSource;
}
}
if (onCompletion != null)
onCompletion();
};
switch (type)
{
case GameType.NewGames:
{
client.DownloadStringAsync(new Uri("http://microsoft.com", UriKind.Absolute));
break;
}
case GameType.ComingSoon:
{
client.DownloadStringAsync(new Uri("http://www.bing.com", UriKind.Absolute));
break;
}
}
}
Although the code might look a bit more complex, it lets you recursively download data when a specific data set is downloaded. Obviously, you would have to declare the GameType enum and I simply used a bunch of test values here to demonstrate the idea.
// It Will Download The XML Once To Use further to Avoid Again And Again Calls For Each Time
public void GetXML(string path)
{
WebClient wcXML = new WebClient();
wcXML.OpenReadAsync(new Uri(path));
wcXML.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient);
}
void webClient(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
try
{
Stream Resultstream = e.Result;
XmlReader reader = XmlReader.Create(Resultstream);
var isolatedfile = IsolatedStorageFile.GetUserStoreForApplication();
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("Download.xml", System.IO.FileMode.Create, isolatedfile))
{
byte[] buffer = new byte[e.Result.Length];
while (e.Result.Read(buffer, 0, buffer.Length) > 0)
{
stream.Write(buffer, 0, buffer.Length);
}
stream.Flush();
System.Threading.Thread.Sleep(0);
}
}
catch (Exception ex)
{
//Log Exception
}
}
if (e.Error != null)
{
//Log Exception
}
}
// This Method Will Give You Required Info According To Your Tag Like "Achievement123"
protected List<DownloadInfo> GetDetailFromXML(string TagName)
{
//TagName Like "achivement23"
List<DownloadInfo> listDetails = new List<DownloadInfo>();
XDocument loadedData;
try
{
using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
{
// Check For Islated Storage And Load From That
if (storage.FileExists("Download.xml"))
{
using (Stream stream = storage.OpenFile("Download.xml", FileMode.Open, FileAccess.Read))
{
loadedData = XDocument.Load(stream);
//TagName Like "achivement23"
listDetails.AddRange((from query in loadedData.Element("GOW1").Elements(TagName)
select new DownloadInfo
{
TITLE = (string)query.Element("TITLE"),
DESCRIPTION = (string)query.Element("DESCRIPTION"),
ACHIVEMENTIMAGE = (string)query.Element("ACHIVEMENTIMAGE"),
GUIDE = (string)query.Element("GUIDE"),
YOUTUBELINK = (string)query.Element("YOUTUBELINK")
}).ToList());
}
}
}
return listDetails;
}
catch (Exception ex)
{
return listDetails = null;
//Log Exception
}
}
public class DownloadInfo
{
public string TITLE { get; set; }
public string DESCRIPTION { get; set; }
public string GUIDE { get; set; }
public string ACHIVEMENTIMAGE { get; set; }
public string YOUTUBELINK { get; set; }
}
Here is Your Method Calls
GetXML("ANY URL FROM WHERE YOU WANT TO GET XML"); // You Will Download All The XML Once To Avoid Again And Again Server Calls To Get XML.
GetDetailFromXML("YOUR TAG NAME"); // It Will Take Your Tag Name Which Information You Want To Get Like "Acheicement123" and It Will Return You Data In List<DownloadInfo> and You can easily get data from this List.
I hope it will help you. I didnot Test The Code on Runtime. But I Hope It Will Give You Some Idea.. :)