2012-04-12 135 views
0

我已经创建了一个“CSV文件”在我的Windows手机, 我想发布它,在服务器,在网络中,我不觉得我想如何继续为此,发送文件在POST请求与Windows Phone 7

我不想只是做一个“POST请求”与参数,我想我张贴在服务器上的文件...

其实,我连接到该服务器,但它没有找到我的文件...

public void SentPostReport() 
    { 


     //Post response. 
     string url = this.CurentReportkPI.configXml.gw; // string url 
     Uri uri = new Uri(url); 

     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); 
     request.Accept = "application/CSV"; 
     request.Method = "POST"; 
     request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request); 
    } 

    private void GetRequestStreamCallback(IAsyncResult asynchronousResult) 
    { 
     HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; 

     // End the operation 
     Stream postStream = request.EndGetRequestStream(asynchronousResult); 

     // I create My csv File 
     CreateCsv reportCsv = new CreateCsv(); 
     string pathReportFile = reportCsv.CreateNewReport(this.report); 
     string CsvContent = reportCsv.ReadFile(pathReportFile); 

     // Convert the string into a byte array. 
     byte[] byteArray = Encoding.UTF8.GetBytes(CsvContent); 

     // Write to the request stream. 
     postStream.Write(byteArray, 0, byteArray.Length); 
     postStream.Close(); 

     // Start the asynchronous operation to get the response 
     request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request); 
    } 


    private static void GetResponseCallback(IAsyncResult asynchronousResult) 
    { 
     Debug.WriteLine("GetResponseCallback"); 
     HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; 

     // End the operation 
     HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult); 
     Stream streamResponse = response.GetResponseStream(); 
     StreamReader streamRead = new StreamReader(streamResponse); 
     string responseString = streamRead.ReadLine(); 
     // Close the stream object 
     streamResponse.Close(); 
     streamRead.Close(); 

     // Release the HttpWebResponse 
     response.Close(); 
    } 

你有一个想法,当我继续解决我的问题,并发送我的CSV文件与我的要求?

谢谢。

回答

1

不知道这是否是这里的问题,但是对于POST请求,您应该设置ContentLength和ContentType(“application/x-www-form-urlencoded”)标题以及其他东西...

请检查this关于完全正确的POST请求的“how-to”文章 - 这不适用于Windows Phone,但我认为您仍然可以获得完整的理念!

另一方面,我建议你去RestSharp,这将为您解决所有这些问题!

+0

Okay for RestSharp!谢谢!! – 2012-04-13 13:24:07

0

您可以使用带有AddFile方法的RestSharp或Hammock轻松完成此操作。以下是我使用Hammock上传照片的一个示例:

var request = new RestRequest("photo", WebMethod.Post); 
request.AddParameter("photo_album_id", _album.album_id); 
request.AddFile("photo", filename, e.ChosenPhoto); 
request.Client.BeginRequest(request, (restRequest, restResponse, userState) => 
    { 
     // handle response 
    } 
+0

感谢您的RestRequest示例,但是,我如何创建并发布文件,只需使用服务器地址的url以及要发送的文件的文件名和url? – 2012-04-13 13:33:40