使用 Windows Phone 7 发送开机自检请求中的文件

本文关键字:请求 文件 开机自检 Windows Phone 使用 | 更新日期: 2023-09-27 18:30:48

我在Windows Phone中创建了一个"csv文件",我想将其发布在服务器中,在网络上,但我找不到我想如何进行,

我不想只发出带有参数的"发布请求",我想在服务器中发布我的文件......

实际上,我正在连接到此服务器,但它找不到我的文件...

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文件与我的请求一起发送时,您有想法吗?

谢谢。

使用 Windows Phone 7 发送开机自检请求中的文件

不确定这是否是这里的问题,但是在POST请求中,您应该设置ContentLength和ContentType("application/x-www-form-urlencoded")标头,以及其他...

请查看这篇关于完全正确的 POST 请求的"操作方法"文章 - 它不适用于 Windows Phone,但我认为您仍然可以获得完整的 ideia!

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

您可以使用

RestSharp或Hammock和AddFile方法轻松完成此操作。以下是我使用吊床上传照片的示例:

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 
    }