Forum

c# asp.net post edi...
 
Bildirimler
Hepsini Temizle

c# asp.net post edilen veriyi lokalde txt metin dosyasına aktarma

ibrahim tamtürk
(@ibrahimtamturk)
Üye

 

Merhaba

Aşağıdaki kodda post edilen veriyi aynı zamanda sitenin lokalinde txt dosyasına nasıl ekleyebilirim. 


public class HttpRequestUtil
{
public string PostRequestOld(string url, string postdata, string contentType,
Dictionary<string, string> headers)
{
System.Net.ServicePointManager.SecurityProtocol =
SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
HttpWebResponse resp = null;
try
{
var request = (HttpWebRequest) WebRequest.Create(url);

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var enc1252 = Encoding.GetEncoding(1252);

var postdatabytes = enc1252.GetBytes(postdata);
request.Method = "POST";
request.ContentType = contentType;
foreach (var header in headers)
request.Headers.Add(header.Key, header.Value);
var requeststream = request.GetRequestStreamAsync().Result;
requeststream.Write(postdatabytes, 0, postdatabytes.Length);
requeststream.Dispose();
//dsada

resp = (HttpWebResponse) request.GetResponseAsync().Result;

var responsereader =
new StreamReader(resp.GetResponseStream());

return responsereader.ReadToEnd(); //Gelen xml string olarak alındı.
}
catch (Exception e)
{
Console.WriteLine(e);
throw new Exception(e.Message);
}
finally
{
resp?.Dispose();
}
}
public static string PostRequest(string url, string postdata, string encoding = "UTF-8")
{
return PostRequest(url, postdata, null, null,encoding);
}
public static string PostRequest(string url, string postdata, string contentType, Dictionary<string, string> headers)
{
return PostRequest(url, postdata, contentType, headers,"UTF-8");
}
private static string PostRequest(string url, string postdata, string contentType,
Dictionary<string, string> headers, string encoding)
{
try
{
ServicePointManager.SecurityProtocol =
SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

WebRequest webRequest = WebRequest.Create(url);
webRequest.Method = "POST";

webRequest.ContentType = contentType ?? "application/x-www-form-urlencoded";
if (headers != null)
foreach (var header in headers)
webRequest.Headers.Add(header.Key, header.Value);

StreamWriter streamWriter = new StreamWriter(webRequest.GetRequestStream());
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

streamWriter.Write(postdata);
streamWriter.Close();
StreamReader streamReader = new StreamReader(webRequest.GetResponse().GetResponseStream(),
Encoding.GetEncoding(encoding), true);
string str = streamReader.ReadToEnd();
streamReader.Close();
return str;
}
catch (Exception e)
{
Console.WriteLine(e);
throw new Exception(e.Message);
}
}

}

Alıntı
Topic starter Gönderildi : 02/10/2018 14:42
Ali UYSAL
(@aliuysal)
Kıdemli Üye Forum Yöneticisi

Merhabalar;

 

protected void TestEvent(object sender, EventArgs e)
{
  using (StreamWriter _testData = new StreamWriter(Server.MapPath("~/data.txt"), true))
 {
  _testData.WriteLine(postdata); // Write the file.
 }         
}

Dijital dönüşüm başlıyor...
WinFlow e-Fatura Onay Akış Süreç Yazılımı
Android El Terminali

CevapAlıntı
Gönderildi : 05/10/2018 20:18
ibrahim tamtürk
(@ibrahimtamturk)
Üye

Teşekkür ederim. 

CevapAlıntı
Topic starter Gönderildi : 02/11/2018 13:51
Paylaş: