2012-04-12 39 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