我想寫一些輸出到我的服務器上的.csv文件。我使用以下代碼,但文件中仍然沒有輸出。 意味着什麼都不寫。 我的代碼是在這裏如何在遠程服務器上寫入CSV文件?
void WriteInRegister(DataRow userdata)
{
HttpWebRequest reqHTTP;
StringBuilder result = new StringBuilder();
try
{
reqHTTP = (HttpWebRequest)HttpWebRequest.Create(new Uri("http://10.10.10.10/config/RequestPlacer.csv"));
reqHTTP.Credentials = new NetworkCredential("usename", "password");
if (reqHTTP != null)
{
reqHTTP.ContentType = "application/octet-stream";
reqHTTP.Method = WebRequestMethods.File.UploadFile;
}
string postdata = (userdata[0].ToString() + ",MacId," + DateTime.Now.ToShortDateString()).ToString();
byte[] text = Encoding.UTF8.GetBytes(postdata);
reqHTTP.ContentLength = text.Length;
/* WebClient wcClient = new WebClient();
wcClient.Credentials = new NetworkCredential("usename", "password");
wcClient.UseDefaultCredentials = true;*/
//wcClient.OpenWrite(new Uri("http://10.10.10.10/config/RequestPlacer.csv"), postdata);
// string path = System.Web.HttpContext.Current.Server.MapPath("http://10.10.10.10/config/RequestPlacer.csv");
reqHTTP.AllowWriteStreamBuffering = true;
Stream writer = reqHTTP.GetRequestStream();
/* wcClient.Headers.Add("Content-Type", "application/csv");
wcClient.UploadString(new Uri("http://10.10.10.10/config/RequestPlacer.csv"), "POST", postdata);*/
StreamWriter streamwriter = new StreamWriter(writer);
streamwriter.WriteLine(postdata);
streamwriter.Close();
}
catch (Exception exe)
{
MessageBox.Show(exe.Message);
}
}
例外是(「請求已中止:請求已被取消」)
你不是在請求調用GetResponse的,所以它不能完成。在哪條線上拋出異常? – 2014-10-04 11:08:06
at streamwriter.Close(); – 2014-10-04 11:11:23
我也試過WebClient,但沒有結果 – 2014-10-04 11:12:02