Convert C# Console application application to Website/WebApp - c#

I've made a console app (don't judge for it being badly written, I can't even do classes properly and I know that RegEx doesn't work with XML/HTML, but let's not mind that I'm not even a programmer)
int retryCount = 0;
int retryCount02 = 0;
int innerpercentage = 1;
HttpWebResponse response = null;
HttpWebResponse response02 = null;
//WebRequest groupingurl = WebRequest.Create("supersecretlink");
WebRequest groupingurl = WebRequest.Create("anothersupersecretlink"); // temp
var groupingurlread = (HttpWebResponse)groupingurl.GetResponse();
using (var groupingreader = new StreamReader(groupingurlread.GetResponseStream(), Encoding.ASCII))
{
string responseGrouping = groupingreader.ReadToEnd();
Console.WriteLine("--------");
var pattern = #"""ClassID"" Value=""(.*?)""";
var matches = Regex.Matches(responseGrouping, pattern)
.OfType<Match>()
.Select(m => m.Groups[1].Value)
.ToArray();
int percentage = 1;
int percentagedone = matches.Length;
foreach (string r in matches)
{
while (true)
{
try
{
var url = "themostseriouslinkever";
string req = File.ReadAllText(#"somanylinksrighthere", Encoding.Default);
var go = url + req;
go = String.Format(go, r);
WebRequest request = WebRequest.Create(go);
response = (HttpWebResponse)request.GetResponse();
break;
}
catch (WebException)
{
Console.WriteLine("Retry No. {0}", retryCount+1);
if (++retryCount < 3) continue;
throw;
}
}
retryCount = 0;
using (var reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII))
{
string responseText = reader.ReadToEnd();
pattern = #"<PartNumber>(.*?)</PartNumber>";
matches = Regex.Matches(responseText, pattern)
.OfType<Match>()
.Select(m => m.Groups[1].Value)
.ToArray();
int innerpercentagedone = matches.Length;
foreach (string bbb in matches)
{
// -----
//Console.WriteLine(bbb);
while (true)
{
try
{
var url02 = "anothersecret";
string req02 = File.ReadAllText(#"seeecreet", Encoding.Default);
var go02 = url02 + req02;
go02 = string.Format(go02, bbb);
WebRequest request02 = WebRequest.Create(go02);
response02 = (HttpWebResponse)request02.GetResponse();
break;
}
catch (WebException)
{
Console.WriteLine("Retry No. {0}", retryCount02+1);
if (++retryCount02 < 3) continue;
throw;
}
}
retryCount02 = 0;
using (var reader02 = new StreamReader(response02.GetResponseStream(), Encoding.ASCII))
{
string responseText02 = reader02.ReadToEnd();
responseText02 = Regex.Replace(responseText02, (#"<\?xml.*\?>"), "");
//Console.WriteLine(responseText02);
//Console.WriteLine("</PartNumber>.*<");
responseText = Regex.Replace(responseText, ((#"<PartNumber>" + bbb + #"<\/PartNumber>")), responseText02);
//Console.WriteLine(responseText);
Console.Clear();
Console.WriteLine("(" + percentage + ") " + innerpercentage + "/" + innerpercentagedone + ". Total: " + percentage + " / " + percentagedone);
innerpercentage++;
}
}
//string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
using (StreamWriter outputFile = new StreamWriter(#"D:\Downloads\edasltxmlrequest\ConsoleApplication1\ConsoleApplication1\bin\Debug\test\" + r + ".xml"))
{
outputFile.WriteLine(responseText);
}
//Console.Clear();
//Console.WriteLine(percentage + " / " + percentagedone);
percentage++;
innerpercentage = 1;
//Console.WriteLine(responseText);
}
}
Console.WriteLine("--------");
Console.ReadLine();
}
}
}
So I wanted to ask if it's possible to make it run as a web app (http://www.amazingwebsite.com/ThisThingIWroteRightThere.php

Related

How to communicate ( NETS Terminal Ingenico ) server using IP and port and get valid response

I have send a valid hex format data to ( NETS Terminal Ingenico ) using IP and port.
But I received a response (NACK - "\u0015" ),its should come response (ACK - "\u0006")
Sample request send : 0200184e455453303931353038313335353031301C033D
Sample text explain in below.
STX : 02 ,
COUNT :0018 ,
ECN : 4e4554533039313530383133 ,
FUCNTION CODE : 3535 ,
VERSION CODE :3031,
RFU : 30 ,
SEPARATOR : 1C,
ETX : 03 ,
LRC : 3D
I am not sure issue in code are data sending format.
try
{
if (txtECN.Text == "Auto" || txtECN.Text == "")
{
txtECN.Text = "NETS" + DateTime.Now.ToString("MMddhhmm");
}
//Message Header Header
var hex_txtECN = GetHex(txtECN.Text);
var hex_txtFuctionCode = GetHex(txtFuctionCode.Text);
var hex_txtVersionCode = GetHex(txtVersionCode.Text);
var hex_txtRFU = GetHex(txtRFU.Text);
var hex_txtSeparator = txtSeparator.Text;
var Req_Header = hex_txtECN + "|" + hex_txtFuctionCode + "|" + hex_txtVersionCode + "|" + hex_txtRFU + "|" + hex_txtSeparator;
var total_bytes = Req_Header.Replace("|", "");
var length = "00" + (total_bytes.Length) / 2;
var Message_Header = length + total_bytes;
//Message Header End
//Message Data start
var hex_txtTranTypeIndicator = GetHex(txtTranTypeIndicator.Text);
var hex_txtAmount = GetHex(txtAmount.Text);
var hex_txtCashAmount = GetHex(txtCashAmount.Text);
var hex_ECRReffNo = GetHex(ECRReffNo.Text);
var hex_txtSeparator2 = txtSeparator2.Text;
var Req_Data = hex_txtTranTypeIndicator + "|" + hex_txtAmount + "|" + hex_txtCashAmount + "|" + hex_ECRReffNo + "|" + hex_txtSeparator2;
var total_bytes1 = Req_Data.Replace("|", "");
var length1 = "00" + (total_bytes1.Length - 1);
var Message_Data = "";
if (txtTranTypeIndicator.Text != "")
{
var fieldCode1 = GetHex("T2");
var val1 = GetHex("02");
var len1 = "0001";
var separator1 = "1C";
var data1 = fieldCode1 + len1 + val1 + separator1;
var fieldCode2 = GetHex("T2");
var val2 = GetHex("000001");
var len2 = "0006";
var separator2 = "1C";
var data2 = fieldCode2 + len2 + val2 + separator2;
Message_Data = data1 + data2;
}
//Message Data send
var Message = Message_Header + Message_Data + txtETX.Text;
txtLRC.Text = LRC(Message).ToString();
var send_command = txtSTX.Text + Message + txtLRC.Text;
txtresponse.Text = txtSTX.Text + " " + length + Req_Header.Replace("|", " ") + " " + Req_Data.Replace("|", " ") + " " + txtETX.Text + " " + txtLRC.Text;
var endPoint = new IPEndPoint(IPAddress.Parse("192.168.0.250"), 3000);
using Socket client = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
await client.ConnectAsync(endPoint);
var messageBytes = Encoding.UTF32.GetBytes(send_command);
_ = await client.SendAsync(messageBytes, SocketFlags.None);
// Receive ack.
var buffer = new byte[1024];
var received = await client.ReceiveAsync(buffer, SocketFlags.None);
var response = char.ConvertFromUtf32(received);
var UTF8 = Encoding.UTF8.GetString(buffer, 0, received);
int sendTry = 0;
int ms = 0;
bool success = false;
while (true)
{
if (receivedACK)
{
success = true;
receivedACK = false;
receivedNACK = false;
break;
}
if (receivedNACK)
{
success = false;
receivedACK = false;
receivedNACK = false;
break;
}
if (ms > 1000)
{
ms = 0;
sendTry++;
if (sendTry == 3)
break;
_ = await client.SendAsync(messageBytes, SocketFlags.None);
// Receive ack.
buffer = new byte[1024];
received = await client.ReceiveAsync(buffer, SocketFlags.None);
response = char.ConvertFromUtf32(received);
UTF8 = Encoding.UTF8.GetString(buffer, 0, received);
if(response == ((char)0x06).ToString())
{
receivedACK = true;
}
else if (response == ((char)0x15).ToString())
{
receivedACK = false;
}
else
{
receivedACK = false;
}
}
System.Threading.Thread.Sleep(1);
ms++;
}
client.Shutdown(SocketShutdown.Both);
}
catch (Exception ex)
{
}

How to retrieve attachments from Gmail message Json [duplicate]

How can I browse the email and download all attachments ?
public string Connect_Email ()
{
string Res = "";
try
{
mailclient = new TcpClient("pop.orange.fr", Convert.ToInt16("110"));
}
catch ( SocketException ExTrhown )
{
Res = "Unable to connect to server 1";
throw new Exception(ExTrhown.Message + "Unable to connect to server 1");
}
ns = mailclient.GetStream();
sr = new StreamReader(ns);
sw = new StreamWriter(ns);
response = sr.ReadLine(); //Get opening POP3 banner
sw.WriteLine("USER " + "xxxxx#orange.fr"); //Send username
sw.Flush();
response = sr.ReadLine();
if ( response.Substring(0, 4) == "-ERR" )
{
Res = "Unable to log into server 2";
}
sw.WriteLine("PASS " + "xxxxx"); //Send password
sw.Flush();
response = sr.ReadLine();
if ( response.Substring(0, 3) == "-ER" )
{
Res = "Unable to log into server 3";
}
return Res;
}
public void Get_Attacht ()
{
string ClientName = "";
#region Chercher Attachment
sw.WriteLine("STAT"); //Send stat command to get number of messages
sw.Flush();
response = sr.ReadLine();
//find number of message
string[] nummess = response.Split(' ');
totmessages = Convert.ToInt16(nummess[1]);
//read emails
for ( int i = 1; i <= totmessages; i++ )
{
msg = null;
sw.WriteLine("top " + i + " 0"); //read header of each message
sw.Flush();
response = sr.ReadLine();
while ( true )
{
response = sr.ReadLine();
if ( response == "." )
break;
msg = msg + response + "\r\n";
}
//read attachment
attachment = null;
if ( Regex.Match(msg, "multipart/mixed").Success )
{
msg = null;
sw.WriteLine("retr " + i.ToString()); //Retrieve entire message
sw.Flush();
response = sr.ReadLine();
while ( true )
{
response = sr.ReadLine();
if ( response == "." )
break;
msg = msg + response + "\r\n";
}
int End = msg.IndexOf(".csv");
string LeFile = msg.Substring(End - 9, 9);
if ( Regex.Match(msg, LeFile + ".csv").Success )
{
data = msg.Split('\r');
startindex = 0;
index = 0;
lastindex = 0;
x = null;
ms = null;
fs = null;
while ( true )
{
attachment = null;
while ( !Regex.Match(data[index].Trim(), "filename").Success )
{
index++;
}
if ( index == data.Length - 1 ) break;
FileName_Email = data[index].Trim().Substring(42).Replace("\"", "");
//find start of attachment data
index++;
while ( data[index].Length != 1 )
{
index++;
}
if ( index == data.Length - 1 ) break;
startindex = index + 1;
//find end of data
index = startindex + 1;
while ( ( !Regex.Match(data[index].Trim(), "--0").Success ) && ( data[index].Length != 1 ) && ( index < data.Length - 1 ) )
{
index++;
}
if ( index == data.Length ) break;
lastindex = index - 2;
for ( int j = startindex; j <= lastindex; j++ )
{
attachment = attachment + data[j];
}
attachment = attachment + "\r\n";
if ( Regex.Match(FileName_Email.ToLower(), "csv").Success )
{
byte[] filebytes = Convert.FromBase64String(attachment);
FileStream LeFS = new FileStream(filePath + "\\testDEC.csv", FileMode.Create, FileAccess.Write, FileShare.None);
LeFS.Write(filebytes, 0, filebytes.Length);
LeFS.Close();
break;
}
}
}
}
}
sw.WriteLine("quit"); //quit
sw.Flush();
#endregion
}
It does not work, have you another simple idea ?
Try something like this
using(Pop3 pop3 = new Pop3())
{
pop3.Connect("server");
pop3.UseBestLogin("user", "password");
foreach (string uid in pop3.GetAll())
{
IMail email = new MailBuilder()
.CreateFromEml(pop3.GetMessageByUID(uid));
Console.WriteLine(email.Subject);
// save all attachments to disk
email.Attachments.ForEach(mime => mime.Save(mime.SafeFileName));
}
pop3.Close();
}
// here is a reference link you can use as well
Getting Email Attachments
If you're trying to read e-mail via POP3, I would recommend using the OpenPOP.NET library instead of rolling your own. It's pretty easy to use.
Thanks you all for your contribution. Finally I use POP3:
public string Connect_Email()
{
string Res = "";
try
{
Pop3Client email = new Pop3Client("login", "password", "server");
email.OpenInbox();
while (email.NextEmail())
{
if (email.IsMultipart)
{
IEnumerator enumerator = email.MultipartEnumerator;
while (enumerator.MoveNext())
{
Pop3Component multipart = (Pop3Component)
enumerator.Current;
if (multipart.IsBody)
{
//Console.WriteLine("Multipart body:" + multipart.Name);
}
else
{
//Console.WriteLine("Attachment name=" + multipart.Name); // ... etc
byte[] filebytes = Convert.FromBase64String(multipart.Data);
//Search FileName
int Begin = multipart.ContentType.IndexOf("name=");
string leFileNale = multipart.ContentType.Substring(Begin + 5, 12);
FileStream LeFS = new FileStream(filePath + "\\" + leFileNale, FileMode.Create, FileAccess.Write, FileShare.None);
LeFS.Write(filebytes, 0, filebytes.Length);
LeFS.Close();
}
}
}
}
email.CloseConnection();
}
catch (Pop3LoginException)
{
Res = "Vous semblez avoir un problème de connexion!";
}
return Res;
}
It work well, but still I have to find and download the attachement my self.
byte[] filebytes = Convert.FromBase64String(multipart.Data);
//Search FileName
int Begin = multipart.ContentType.IndexOf("name=");
string leFileNale = multipart.ContentType.Substring(Begin + 5, 12);
FileStream LeFS = new FileStream(filePath + "\\" + leFileNale, FileMode.Create, FileAccess.Write, FileShare.None);
LeFS.Write(filebytes, 0, filebytes.Length);
LeFS.Close();
This not work more. Use Modern Authentication with Microsoft.Graph after create a cliente secret into AZURE and registered tha application them.

Downloading file from redirecting URLs

I am trying to download mp3 from http://www.audiodump.com/. The site has a lot of redirections. However I managed getting a part of it working.
This is my method for getting all informations such as DL links, titles, mp3 durations.
private void _InetGetHTMLSearch(string sArtist)
{
if(_AudioDumpQuery == string.Empty)
{
//return string.Empty;
}
string[] sStringArray;
string sResearchURL = "http://www.audiodump.biz/music.html?" + _AudioDumpQuery + sArtist.Replace(" ", "+");
string aRet;
HttpWebRequest webReq = (HttpWebRequest)HttpWebRequest.Create(sResearchURL);
webReq.Referer = "http://www.audiodump.com/";
try
{
webReq.CookieContainer = new CookieContainer();
webReq.Method = "GET";
using (WebResponse response = webReq.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream);
aRet = reader.ReadToEnd();
//Console.WriteLine(aRet);
string[] aTable = _StringBetween(aRet, "<BR><table", "table><BR>", RegexOptions.Singleline);
if (aTable != null)
{
string[] aInfos = _StringBetween(aTable[0], ". <a href=\"", "<a href=\"");
if (aInfos != null)
{
for(int i = 0; i < aInfos.Length; i++)
{
aInfos[i] = aInfos[i].Replace("\">", "*");
aInfos[i] = aInfos[i].Replace("</a> (", "*");
aInfos[i] = aInfos[i].Remove(aInfos[i].Length - 2);
sStringArray = aInfos[i].Split('*');
aLinks.Add(sStringArray[0]);
aTitles.Add(sStringArray[1]);
sStringArray[2] = sStringArray[2].Replace("`", "'");
sStringArray[2] = sStringArray[2].Replace("dont", "don't");
sStringArray[2] = sStringArray[2].Replace("lets", "let's");
sStringArray[2] = sStringArray[2].Replace("cant", "can't");
sStringArray[2] = sStringArray[2].Replace("shes", "she's");
sStringArray[2] = sStringArray[2].Replace("aint", "ain't");
sStringArray[2] = sStringArray[2].Replace("didnt", "didn't");
sStringArray[2] = sStringArray[2].Replace("im", "i'm");
sStringArray[2] = sStringArray[2].Replace("youre", "you're");
sStringArray[2] = sStringArray[2].Replace("ive", "i've");
sStringArray[2] = sStringArray[2].Replace("youll", "you'll");
sStringArray[2] = sStringArray[2].Replace("'", "'");
sStringArray[2] = sStringArray[2].Replace("'", "simplequotes");
sStringArray[2] = sStringArray[2].Replace("vk.com", "");
sStringArray[2] = _StringReplaceCyrillicChars(sStringArray[2]);
sStringArray[2] = Regex.Replace(sStringArray[2], #"<[^>]+>| ", "").Trim();
sStringArray[2] = Regex.Replace(sStringArray[2], #"\s{2,}", " ");
sStringArray[2] = sStringArray[2].TrimStart('\'');
sStringArray[2] = sStringArray[2].TrimStart('-');
sStringArray[2] = sStringArray[2].TrimEnd('-');
sStringArray[2] = sStringArray[2].Replace("- -", "-");
sStringArray[2] = sStringArray[2].Replace("http", "");
sStringArray[2] = sStringArray[2].Replace("www", "");
sStringArray[2] = sStringArray[2].Replace("mp3", "");
sStringArray[2] = sStringArray[2].Replace("simplequotes", "'");
aDurations.Add(sStringArray[2]);
}
}
else
{
//Console.WriteLine("Debug");
}
}
else
{
//Console.WriteLine("Debug 2");
}
//return aRet;
}
}
}
catch (Exception ex)
{
//return null;
////Console.WriteLine("Debug message: " + ex.Message);
}
}
I simply had to add referrer to prevent the search from redirection webReq.Referer = "http://www.audiodump.com/";
However when I want to download the mp3 I can't get it working. The urls are correct and checked with the ones I get when I download them manually rather than programmatically.
This is my mp3 download part:
private void _DoDownload(string dArtist, ref string dPath)
{
if (!Contain && skip <= 3 && !Downloading)
{
Random rnd = new Random();
int Link = rnd.Next(5);
_InetGetHTMLSearch(dArtist);
Console.WriteLine("--------------------------------> " + aLinks[0]);
string path = mp3Path + "\\" + dArtist + ".mp3";
if (DownloadOne(aLinks[Link], path, false))
{
hTimmer.Start();
Downloading = true;
}
}
else if (Downloading)
{
int actualBytes = strm.Read(barr, 0, arrSize);
fs.Write(barr, 0, actualBytes);
bytesCounter += actualBytes;
double percent = 0d;
if (fileLength > 0)
percent =
100.0d * bytesCounter /
(preloadedLength + fileLength);
label1.Text = Math.Round(percent).ToString() + "%";
if (Math.Round(percent) >= 100)
{
string path = mp3Path + "\\" + dArtist + ".mp3";
label1.Text = "";
dPath = path;
aLinks.Clear();
hTimmer.Stop();
hTimmer.Reset();
fs.Flush();
fs.Close();
lastArtistName = "N/A";
Downloading = false;
}
if (Math.Round(percent) <= 1)
{
if (hTimmer.ElapsedMilliseconds >= 3000)
{
string path = mp3Path + "\\" + dArtist + ".mp3";
hTimmer.Stop();
hTimmer.Reset();
fs.Flush();
fs.Close();
File.Delete(path);
Contain = false;
skip += 1;
Downloading = false;
}
}
}
}
private static string ConvertUrlToFileName(string url)
{
string[] terms = url.Split(
new string[] { ":", "//" },
StringSplitOptions.RemoveEmptyEntries);
string fname = terms[terms.Length - 1];
fname = fname.Replace('/', '.');
return fname;
} //ConvertUrlToFileName
private static long GetExistingFileLength(string filename)
{
if (!File.Exists(filename)) return 0;
FileInfo info = new FileInfo(filename);
return info.Length;
} //GetExistingFileLength
private static bool DownloadOne(string url, string existingFilename, bool quiet)
{
ServicePointManager.DefaultConnectionLimit = 20;
HttpWebRequest webRequest;
HttpWebResponse webResponse;
IWebProxy proxy = null; //SA???
//fmt = CreateFormat(
//"{0}: {1:#} of {2:#} ({3:g3}%)", "#");
try
{
fname = existingFilename;
if (fname == null)
fname = ConvertUrlToFileName(url);
if (File.Exists(existingFilename))
{
File.Delete(existingFilename);
}
webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A";
webRequest.Referer = "http://www.audiodump.com/";
preloadedLength = GetExistingFileLength(fname);
if (preloadedLength > 0)
webRequest.AddRange((int)preloadedLength);
webRequest.Proxy = proxy; //SA??? or DefineProxy
webResponse = (HttpWebResponse)webRequest.GetResponse();
fs = new FileStream(fname, FileMode.Append, FileAccess.Write);
fileLength = webResponse.ContentLength;
strm = webResponse.GetResponseStream();
if (strm != null)
{
bytesCounter = preloadedLength;
return true;
}
else
{
return false;
}
}
catch (Exception e)
{
//Console.WriteLine(
//"{0}: {1} '{2}'",
// url, e.GetType().FullName,
//e.Message);
return false;
}
//exception
} //DownloadOne
The method _DoDownload() is executed from a timer which runs every 250 milliseconds. This way works perfectly on other sites. However audiodump is giving me hard time with these redirections.
I am not a genius with httprequest. I managed solving the search issue however the download part is freaking me out. Any advice on how to manage the download issue?
You just need to set referrer to the page from where you got that download link. For example you grabbed links to files from page "http://www.audiodump.biz/music.html?q=whatever", then when downloading file set that as Referrer, not just "http://www.audiodump.biz".

Read Credentials by txt file and try login c#?

I would create a loop for function and print message in catch for all logins failed and print "sucessful connected with...." for all good logins.
Now i tried with that code, but i get only one error in textbox of catch
Button1:
if (openFileDialog1.FileName != string.Empty)
{
using (StreamReader reader = new StreamReader(openFileDialog1.FileName))
{
int count = 0;
string lineoflistemail;
while ((lineoflistemail = reader.ReadLine()) != null)
{
UserData d = new UserData();
string[] parts = lineoflistemail.Split(':');
count = parts.Length;
d.UserName = parts[0].Trim();
d.Password = parts[1].Trim();
data.Add(d);
}
foreach(UserData ud in data)
{
textBox1.Text += ("LOL" + ud.UserName + ud.Password + Environment.NewLine);
}
Second button code:
if (data.Count() == 0)
{
MessageBox.Show("Load user info first");
return;
}
for( hola = 0; hola < data.Count(); hola++)
{
var url = #"https://mail.google.com/mail/feed/atom";
var encoded = TextToBase64(data[0].UserName + ":" + data[1].Password);
var myweb = HttpWebRequest.Create(url) as HttpWebRequest;
myweb.Method = "POST";
myweb.ContentLength = 0;
myweb.Headers.Add("Authorization", "Basic " + encoded);
var response = myweb.GetResponse();
var stream = response.GetResponseStream();
textBox1.Text += ("Connection established with");
MessageBox.Show(hola.ToString());
}
}
catch (Exception ex)
{
textBox1.Text += ("Error connection. Original error: " + ex.Message);
}
}
The problem is that your try...catch is outside of your for loop. The first exception will exit the for loop, append the message to textBox1, then exit the button handler.
If you want to keep looping through even if there is an error, move the try...catch inside of the loop. Here's an example:
for( hola = 0; hola < data.Count(); hola++)
{
var url = #"https://mail.google.com/mail/feed/atom";
var encoded = TextToBase64(data[0].UserName + ":" + data[1].Password);
var myweb = HttpWebRequest.Create(url) as HttpWebRequest;
myweb.Method = "POST";
myweb.ContentLength = 0;
myweb.Headers.Add("Authorization", "Basic " + encoded);
try
{
var response = myweb.GetResponse();
var stream = response.GetResponseStream();
textBox1.Text += ("Connection established with");
MessageBox.Show(hola.ToString());
}
catch (Exception ex)
{
textBox1.Text += ("Error connection. Original error: " + ex.Message);
}
}

System.OutOfMemoryException in thread

I have a problem. This part of code returns after ~1min System.OutOfMemoryException error. But I can't find what exacly causes this error. I would be glad if somebody will tell me what is the reason of this issue, and how to resolve it.
Part of my thread:
public void Run(object client)
{
TextBox tbServerResult = (client as Client).Controls.Find("tbServerResult", true).SingleOrDefault() as TextBox;
Client tt = new Client();
for (int i = 0; i < 1; i--)
{
Thread.Sleep(10000);
string result = tt.SendGet("xyz" + tt.getToken() + "");
tbServerResult.AppendText(result);
}
}
SendGet method:
public string SendGet(string url)
{
string webpageContent = "";
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
using (HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(webResponse.GetResponseStream()))
{
webpageContent = reader.ReadToEnd();
}
//tbServerResult.AppendText("\r\n" + "Server code:" + webResponse.StatusCode + " Server status description:" + webResponse.StatusDescription);
webpageContent = "Server code:" + webResponse.StatusCode + " Server status description:" + webResponse.StatusDescription;
}
}
catch (Exception ex)
{
webpageContent = ex.Message;
}
return webpageContent;
}
You have line with for (int i = 0; i < 1; i--)
this is infinite loop.

Categories