0

I have create an Csv File in my phone ( I have this content and this path ) and, url to post this csv...

string path = csv.path;
string content = csv.content;
string urlPost = csv.urlPost;

I want to post my File ( just one csv file ) with the HttpWebRequest, for windows phone, I saw lot of post for HttpWebRequest in C# , and never specific to windows Phone ( HttpWebRequest for windows phone doesn't have all method of "normal" HttpWebRequest) .

I have already saw the msdn page => http://msdn.microsoft.com/en-us/library/debx8sh9.aspx and this post for HttpWebRequest in c# Upload files with HTTPWebrequest (multipart/form-data) But I don't arrived to translate examples for windows phone.

I arrived to called my server ( with my Url) but, the file are never transmitted...

Also, I don't want use the RestSharp library.

My actually code =>

public void SentPostReport()
    {

        Uri uri = new Uri(csv.urlPost); //Url is string url 

        string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
        request.ContentType = "multipart/form-data; boundary=" + boundary;
        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);

        // My File content and path.
        string pathReportFile = csv.path;
        string CsvContent = csv.content;


        // Transmitted The File ???? 

        postStream.Close();

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


    private static void GetResponseCallback(IAsyncResult asynchronousResult)
    {
        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();
            //DEBUG => affiche le résultat de la requête.
            Debug.WriteLine(responseString);
        // Close the stream object
        streamResponse.Close();
        streamRead.Close();

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

I have delete my code in the GetRequestStreamCallback's function , and replace by "// transmitted file ? "

I think it's the places where I have to send my file, but I don't find the solution to transmitted this.

I have testing with this code :

byte[] byteArray = Encoding.UTF8.GetBytes(CsvContent);
postStream.Write(byteArray, 0, byteArray.Length);

and other found in different forum, and I every times, teh call with my server is good, but file isn't transmitted...

Have you a solution for use HttpWebRequest and Stream for send my file in my server? In advance : thanks!!

Community
  • 1
  • 1
Doc Roms
  • 3,288
  • 1
  • 20
  • 37

1 Answers1

0

Well, I just found this project, is just a single helper class, is very simple and helpful.

MultipartHttpClient

Hope this helps

hmartinezd
  • 1,188
  • 8
  • 11