我在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文件?
感谢。
答案 0 :(得分:1)
不确定这是否是问题所在,但在POST请求中,您应该设置ContentLength和ContentType(“application / x-www-form-urlencoded”)标头等等......
请查看this“操作方法”关于完全正确的POST请求的文章 - 这不适用于Windows Phone,但我认为您仍然可以获得完整的意识形态!
另一方面,我建议您选择RestSharp来解决所有这些问题!
答案 1 :(得分: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
}