Send Word File Stream Web Service Request and Resthttp://forums.asp.net/t/1787384.aspx/1?Send+Word+File+Stream+Web+Service+Request+and+RestMon, 02 Apr 2012 10:28:34 -040017873844908625http://forums.asp.net/p/1787384/4908625.aspx/1?Send+Word+File+Stream+Web+Service+Request+and+RestSend Word File Stream Web Service Request and Rest <p><span class="hps">Hello everyone</span><span>&nbsp;</span><span class="hps">I would like to</span><span>&nbsp;</span><span class="hps">send</span><span>&nbsp;</span><span class="hps">a word file</span><span>&nbsp;</span><span class="hps">in the HTTP</span><span>&nbsp;</span><span class="hps">post</span><span>,</span><span>&nbsp;</span><span class="hps">and</span><span>&nbsp;</span><span class="hps">encode it</span><span>&nbsp;</span><span class="hps">in base64</span><span>.</span><br> <br> <span></span><span class="hps">On the other hand</span><span>&nbsp;</span><span class="hps">I would</span><span>&nbsp;</span><span class="hps">decode it and</span><span>&nbsp;</span><span class="hps">save it</span><span>.</span><br> <br> <span></span><span class="hps">Part</span><span>&nbsp;</span><span class="hps">of code to</span><span>&nbsp;</span><span class="hps">send</span><span>&nbsp;</span><span class="hps">is this:</span></p> <pre class="prettyprint">try { string file = @&quot;C:\Users\Giorgio\Documents\Visual Studio 2010\Projects\TeamTask\procedure.docx&quot;; HttpWebRequest httpRequest = (HttpWebRequest) WebRequest.Create(&quot;http://localhost:8080/TeamTask/&quot;); httpRequest.Method = WebRequestMethods.Http.Post; string postData = &quot;value=&quot; &#43; (Encode(file)); httpRequest.ContentLength = postData.Length; httpRequest.ContentType = &quot;application/x-www-form-urlencoded&quot;; StreamWriter requestWriter = new StreamWriter( httpRequest.GetRequestStream(), System.Text.Encoding.ASCII); requestWriter.Write(postData); requestWriter.Close(); } catch (System.Net.WebException ex) { Console.WriteLine(ex.Message); }</pre> <p><span class="hps"><span class="hps">the</span><span>&nbsp;</span><span class="hps">decoding part</span><span>&nbsp;</span><span class="hps">is the following (WEB SERVICE REST)</span></span></p> <pre class="prettyprint"> [WebInvoke(UriTemplate = "", Method = "PUT")] public String Post() { string value = HttpContext.Current.Request["value"]; Decode(value); return value; } public void Decode(string value) { string file = @"C:\Users\Giorgio\Documents\Visual Studio 2010\Projects\TeamTask\temp.docx"; if (!string.IsNullOrEmpty(file)) { if (File.Exists(file)) File.Delete(file); byte[] filebytes = Convert.FromBase64String(value); FileStream fs = new FileStream(file, FileMode.CreateNew, FileAccess.Write, FileShare.Delete); fs.Write(filebytes, 0, filebytes.Length); fs.Close(); } } </pre> <p>The word file is 47kb, when I send and decode web service from Rest of me is46kb.<br> It seems that you lose something.<br> Who can help me?</p> <p>Ps sorry for my bad English</p> 2012-03-30T15:25:20-04:004911041http://forums.asp.net/p/1787384/4911041.aspx/1?Re+Send+Word+File+Stream+Web+Service+Request+and+RestRe: Send Word File Stream Web Service Request and Rest <p>Hi StefanoIta,</p> <p>I assume that the &quot;Encode&quot; function in your case just simply call Convert.ToBase64String agains the raw byte[] retrieved from FileStream. And the &quot;Decode&quot; function in your service code seems ok.</p> <p>Now, the issue seems focus on how the base64 string get sent out and received at service side. currently you use the following code at the WebRequest based client:</p> <pre class="prettyprint">================== string postData = &quot;value=&quot; &#43; (Encode(file)); httpRequest.ContentLength = postData.Length; httpRequest.ContentType = &quot;application/x-www-form-urlencoded&quot;; StreamWriter requestWriter = new StreamWriter( httpRequest.GetRequestStream(), System.Text.Encoding.ASCII); requestWriter.Write(postData); requestWriter.Close(); ===============================</pre> <p><br />I'd suggest you change it to:</p> <pre class="prettyprint">=========================== string postData = "value=" + (Encode(file)); httpRequest.ContentType = "application/x-www-form-urlencoded"; StreamWriter requestWriter = new StreamWriter( httpRequest.GetRequestStream(), System.Text.Encoding.UTF8); requestWriter.Write(postData); requestWriter.Close(); ========================== </pre> <p><br> So in shorts, you do not need to explicitly set the &quot;ContentLength&quot;, and the ContentLength should be the number of underlying byte sent out(rather than the string length before they're write out as raw Asciior UTF-8 encoded bytes).&nbsp; Also, suggest that we use UTF-8 for webrequest's content encoding so that we do no need to worry about whether the text data we sent are ascii or unicode chars.</p> <p>In addition, here you haven't defined any input parameter in your service operation, but use the HttpContext to retrieve the uploaded file stream in service operation code. I don't think it is the recommended approach for WCF service operation. Actually, with REST style WCF service, you can simply define the operation parameter of System.IO.Stream type and send any data (binary, text or even multi-part form) you like. Here are some references on sending arbitrary data for WCF REST service:</p> <p>#WCF &quot;Raw&quot; programming model (Web) - receiving arbitrary data <br> <a href="http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx">http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx</a></p> <p>#How to: Create a Service That Accepts Arbitrary Data using the WCF REST Programming Model <br> <a href="http://msdn.microsoft.com/en-us/library/cc656724.aspx">http://msdn.microsoft.com/en-us/library/cc656724.aspx</a></p> <p>&nbsp;</p> <p></p> 2012-04-02T06:28:50-04:004911151http://forums.asp.net/p/1787384/4911151.aspx/1?Re+Send+Word+File+Stream+Web+Service+Request+and+RestRe: Send Word File Stream Web Service Request and Rest <p><span class="hps">I</span><span>&nbsp;</span><span class="hps">tried</span><span>&nbsp;</span><span class="hps">the change</span><span>&nbsp;</span><span class="hps">but not</span><span>&nbsp;</span><span class="hps">change anything</span><span>:</span><span>&nbsp;</span><span class="hps atn">(</span><br> <span></span><span class="hps">On the one hand</span><span>&nbsp;</span><span class="hps">I have that</span><span>&nbsp;</span><span class="hps">file is</span><span>&nbsp;</span><span class="hps">47194</span><span>&nbsp;</span><span class="hps">bytes and</span><span>&nbsp;</span><span class="hps">the other I</span><span>&nbsp;</span><span class="hps">get</span><span>&nbsp;</span><span class="hps">the</span><span>&nbsp;</span><span class="hps">46519</span><span>.</span><br> <br> <span></span><span class="hps">I used</span><span>&nbsp;</span><span class="hps">HttpContext</span><span>&nbsp;</span><span class="hps">to retrieve values</span><span>&nbsp;</span><span class="hps">because:</span><br> <span></span><span class="hps">1.Queste</span><span>&nbsp;</span><span class="hps">wsRest</span><span>&nbsp;</span><span class="hps">should</span><span>&nbsp;</span><span class="hps">then</span><span>&nbsp;</span><span class="hps">be called</span><span>&nbsp;</span><span class="hps">from Java</span><br> <span></span><span class="hps">2.Ho</span><span>&nbsp;</span><span class="hps">tried to add</span><span>&nbsp;</span><span class="hps">parameters</span><span>&nbsp;</span><span class="hps">public void</span><span>&nbsp;</span><span class="hps">Post</span><span>&nbsp;</span><span class="hps">()</span><span>,</span><span>&nbsp;</span><span class="hps">but</span><span>&nbsp;</span><span class="hps">as soon as</span><span>&nbsp;</span><span class="hps">I get them</span><span>&nbsp;</span><span class="hps">to send</span><span></span><span class="hps">in</span><span>&nbsp;</span><span class="hps">my test</span><span>&nbsp;</span><span class="hps">post</span><span>&nbsp;</span><span class="hps">(I created</span><span>&nbsp;</span><span class="hps">a</span><span>&nbsp;</span><span class="hps">console</span><span>&nbsp;</span><span class="hps">application</span><span>) tells me</span><span>&nbsp;</span><span class="hps">Error</span><span>&nbsp;</span><span class="hps">400.</span></p> 2012-04-02T07:17:37-04:004911541http://forums.asp.net/p/1787384/4911541.aspx/1?Re+Send+Word+File+Stream+Web+Service+Request+and+RestRe: Send Word File Stream Web Service Request and Rest <p>Thanks for the quick reply StefanoIta,</p> <p>I just performed a quick test through a VS 2010 web project. Here is test REST service I used (I use an operation parameter to carray the base 64 encoded file content)</p> <p>&nbsp;</p> <pre class="prettyprint">// Service Contract [ServiceContract] public interface IRESTFileService { [WebInvoke] [OperationContract] void UploadFile(string base64FileContent); [WebGet] string GetData(string id); }</pre> <p></p> <p>&nbsp;</p> <pre class="prettyprint">// Service Implementation public class RESTFileService : IRESTFileService { public void UploadFile(string base64FileContent) { var path = System.Web.Hosting.HostingEnvironment.MapPath("~/UploadedFiles/test.docx"); if (File.Exists(path)) File.Delete(path); var bytes = Convert.FromBase64String(base64FileContent); using (var fs = File.Create(path)) { fs.Write(bytes, 0, bytes.Length); } } public string GetData(string id) { return "Data for [" + id + "]"; } } </pre> <p></p> <p>&nbsp;</p> <p>// Service Configuration</p> <pre class="prettyprint"> &lt;system.serviceModel&gt; &lt;services&gt; &lt;service name="BinaryRESTService.RESTFileService"&gt; &lt;endpoint name="" behaviorConfiguration="webHttpBehavior" binding="webHttpBinding" contract="BinaryRESTService.IRESTFileService" /&gt; &lt;/service&gt; &lt;/services&gt; &lt;bindings&gt; &lt;webHttpBinding&gt; &lt;binding&gt; &lt;security mode="None"&gt;&lt;/security&gt; &lt;readerQuotas maxStringContentLength="99999999" maxArrayLength="99999999" /&gt; &lt;/binding&gt; &lt;/webHttpBinding&gt; &lt;/bindings&gt; &lt;behaviors&gt; &lt;endpointBehaviors&gt; &lt;behavior name="webHttpBehavior" &gt; &lt;webHttp helpEnabled="true" faultExceptionEnabled="true"/&gt; &lt;/behavior&gt; &lt;/endpointBehaviors&gt; &lt;serviceBehaviors&gt; &lt;behavior name=""&gt; &lt;serviceMetadata httpGetEnabled="true" /&gt; &lt;serviceDebug includeExceptionDetailInFaults="true" httpHelpPageEnabled="true" /&gt; &lt;/behavior&gt; &lt;/serviceBehaviors&gt; &lt;/behaviors&gt; &lt;serviceHostingEnvironment multipleSiteBindingsEnabled="true" /&gt; &lt;/system.serviceModel&gt;</pre> <p></p> <p>&nbsp;</p> <p>&nbsp;</p> <p>// client code</p> <pre class="prettyprint"> static void Main(string[] args) { var request = (HttpWebRequest)WebRequest.Create("http://ipv4.fiddler:33994/RESTFileService.svc/UploadFile"); request.Method = "POST"; request.ContentType = "text/xml"; using (var stream = request.GetRequestStream()) { var bytes = File.ReadAllBytes(@"..\..\test.docx"); var base64Bytes = Convert.ToBase64String(bytes); var requestXML = "&lt;string xmlns='http://schemas.microsoft.com/2003/10/Serialization/'&gt;" + base64Bytes +"&lt;/string&gt;"; var requestBytes = Encoding.UTF8.GetBytes(requestXML); stream.Write(requestBytes, 0, requestBytes.Length); } var response = request.GetResponse() as HttpWebResponse; using (var sr = new StreamReader(response.GetResponseStream())) { Console.WriteLine(sr.ReadToEnd()); } } </pre> <p></p> <p>&nbsp;</p> <p>BTW, as you can see, I've enabled &quot;helpPage&quot; , &quot;faultException&quot; and &quot;includeExceptionDetailInFaults&quot; options which are really helpful for testing and debugging REST service. and you can use fiddler to watch the error info returned in message.</p> <p>One error I got at first is the content length (maxString length) exceed the default limit, and I adjust the quota in the binding configuration(see config fragment above).</p> <p>If you feel necessary, I can send you the entire test solution.</p> 2012-04-02T10:28:34-04:00