C# 使用Windows phone 7在POST请求中发送文件
我已经在我的windows phone中创建了一个“csv文件”, 我想把它放在服务器上,放在网络上,但我不知道该怎么做 我不想只做一个带有参数的“post请求”,我想在服务器上发布我的文件 实际上,我正在连接此服务器,但它找不到我的文件C# 使用Windows phone 7在POST请求中发送文件,c#,windows-phone-7,C#,Windows Phone 7,我已经在我的windows phone中创建了一个“csv文件”, 我想把它放在服务器上,放在网络上,但我不知道该怎么做 我不想只做一个带有参数的“post请求”,我想在服务器上发布我的文件 实际上,我正在连接此服务器,但它找不到我的文件 public void SentPostReport() { //Post response. string url = this.CurentReportkPI.configXml.gw; // string ur
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文件与我的请求一起发送时,你知道吗
谢谢。不确定这是否是问题所在,但在POST请求中,您应该设置ContentLength和ContentType(“application/x-www-form-urlencoded”)头以及其他内容 请在一篇完全正确的帖子请求上查看“操作”文章——这篇文章不适用于Windows Phone,但我认为您仍然可以获得完整的ideia
另一方面,我建议你去做,这将为你解决所有这些问题 您可以使用RestSharp或吊床和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
}
感谢您的RestRequest示例,但是,我如何创建和发布一个文件,其中只包含服务器地址的url,以及要发送的文件名和url?