0

我已经在我的 Windows 手机中创建了一个“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 文件与我的请求一起发送时,你有什么想法吗?

谢谢。

4

2 回答 2

1

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

请检查这篇关于完全正确的 POST 请求的“操作方法”文章——它不适用于 Windows Phone,但我认为你仍然会得到完整的想法!

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

于 2012-04-12T13:36:05.390 回答
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 
    }
于 2012-04-12T16:38:57.067 回答