1 namespace Uploader 2 { 3 using System; 4 using System.IO; 5 using System.ServiceModel; 6 using System.ServiceModel.Description; 7 using System.ServiceModel.Web; 8 using System.Drawing; 9 using System.Drawing.Imaging; 10 using System.Net; 11 using System.Xml; 12 13 [ServiceContract(Namespace = "http://Uploader")] 14 public interface IUploaderService 15 { 16 [OperationContract, WebInvoke(Method = "POST",UriTemplate = "File/{fileName}")] 17 bool UploadFile(string fileName, Stream fileContents); 18 } 19 20 [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] 21 public class UploaderService : IUploaderService 22 { 23 public bool UploadFile(string fileName, Stream fileContents) 24 { 25 return true; 26 } 27 } 28 29 class Program 30 { 31 static void Main() 32 { 33 var host = new 34 ServiceHost(typeof (UploaderService), 35 new Uri("http://localhost:8080/Uploader")); 36 host.AddServiceEndpoint("Uploader.IUploaderService", 37 new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior()); 38 try 39 { 40 host.Open(); 41 Console.WriteLine(host.BaseAddresses[0].AbsoluteUri + " running."); 42 Console.WriteLine(); 43 var uri = "http://localhost:8080/Uploader/file.jpg"; 44 var req = WebRequest.Create(uri) as HttpWebRequest; 45 if (req != null) 46 { 47 req.Method = "POST"; 48 req.ContentType = "image/jpeg"; 49 var reqStream = req.GetRequestStream(); 50 51 var imageStream = new MemoryStream(); 52 using (var i = Image.FromFile(@"c:\photo.jpg")) 53 i.Save(imageStream, ImageFormat.Jpeg); 54 55 var imageArray = imageStream.ToArray(); 56 reqStream.Write(imageArray, 0, imageArray.Length); 57 reqStream.Close(); 58 var resp = (HttpWebResponse)req.GetResponse(); 59 var r = new XmlTextReader(resp.GetResponseStream()); 60 if (r.Read()) 61 { 62 Console.WriteLine(r.ReadString()); 63 } 64 } 65 Console.WriteLine("Press <ENTER> to quit."); 66 Console.ReadLine(); 67 } 68 catch (Exception ex) 69 { 70 Console.WriteLine(ex.Message); 71 Console.ReadKey(); 72 } 73 finally 74 { 75 if (host.State == CommunicationState.Faulted) 76 host.Abort(); 77 else 78 host.Close(); 79 } 80 } 81 } 82 } 83 84
Hi, I hope you can help ....
I am creating a simple application (possibly a web page) that will have a simple interface and will download files from an external device, the application / web page will be launched through autorun.inf when the user connects the device to the PC. Webservice will do the hard work of linking the file to the management system, etc. This will allow illiterate IT users who cannot use file verification to send files to the management system ...!
The problem is that my RESTful serivce gives me a 400 error when the content type is the image / jpeg .. It works fine for text / plain or text / xml (see. Blog article).
Thanks J
c # rest wcf
jaimie
source share