Okay someone said to put my proxies in a list so I can use a new proxy for each request, I have the proxies saved to a list but I am a little confused on where to go from here
This is the code that I used to generate the proxie list, and it's coming from a proxies.txt file.
private List<string> getProxyListFromText(string input) {
List<string> list = new List<string>();
StreamReader reader = new StreamReader(input);
string item = "";
while (item != null)
{
item = reader.ReadLine();
if (item != null)
{
list.Add(item);
}
}
reader.Close();
return list;
}
ok here is the request, each request should retreive a different proxie from the list.
Picture a for loop that is looping through a list of names, each name brings up a different request, and each request should have it's own proxy, the proxy list is already generated in the code above just need a way i can retreive proxies from the list.
for (int i = 0; i < listBox1.Items.Count; i++)
{
object url;
WebClient wc;
url = getppl;
wc = new WebClient();
//This should come from the proxy list
wc.Proxy = new WebProxy(getProxyListFromText("Proxies.txt"));
var page = wc.DownloadString(url.ToString());
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(page);
var pplname = doc.DocumentNode.SelectNodes("/html/body/div[3]/div/div[2]/div[2]/div/div[4]/p");
}
I tried a nested for loop but the logic got tied up somewhere.
Change to:-
foreach(string prox in getProxyListFromText("Proxies.txt"))
{
...
wc.Proxy = new WebProxy(prox);
...
}
Related
I want to ping the hostnames that are in the csv and write the result in the next column, but I'm little bit lost how to do it?
This the error I get:Error CS0021 Cannot apply indexing with [] to an expression of type 'StreamReader'
And the only thing I can do is write to the console what is in the csv.
string filePath = #"c:\hostnames.csv";
var reader = new StreamReader(filePath);
Ping ping = new Ping();
List<string> hostnames = new List<string>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
hostnames.Add(values[0]);
hostnames.ForEach(Console.WriteLine);
}
List<string> goodPing = new List<string>();
foreach (string singleComputer in hostnames)
{
PingReply pingresult = ping.Send(singleComputer);
if (pingresult.Status.ToString() == "Success")
{
goodPing.Add(singleComputer);
}
}
var csv = new StringBuilder();
var first = reader[0].ToString();
var newLine = string.Format("{0}");
csv.AppendLine(newLine);
File.WriteAllText(filePath, csv.ToString());
}
It is not clear what error you are getting is but one issue I noticed is you are not providing a variable to string.Format which I think it is useless at there..
Example hostnames.csv is something like this I assume:
www.google.com,www.somebadu-rlwhichcannot..com,www.stackoverflow.com
var filePath = #"c:\test\hostnames.csv"; // change it to your source
var filePathOutput = #"c:\test\hostnamesOutput.csv"; // use separate output file so you would not overwrite your source..
var ping = new Ping();
var hostNames = new List<string>();
using (var reader = new StreamReader(filePath))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
hostNames = line.Split(',').ToList();
}
}
var goodPing = new List<string>();
foreach (var singleComputer in hostNames)
try
{
var pingResult = ping.Send(singleComputer);
if (pingResult.Status == IPStatus.Success)
{
goodPing.Add(singleComputer);
}
}
catch (Exception e)
{
// host was not in a correct format or any other exception thrown....
// do whatever error handling you want, logging etc...
}
var csv = new StringBuilder();
foreach (var hostname in hostNames)
{
var resultText = goodPing.Contains(hostname) ? "Success" : "Failed";
var newLine = string.Format("{0},{1}", hostname, resultText);
csv.AppendLine(newLine);
}
File.WriteAllText(filePathOutput, csv.ToString());
I didn't try on IDE but it should be doing what you are trying to do. Seems you copy pasted that code from somewhere and tried to manipulate it without understanding. I would suggest to make sure you understand it line by line why it is used, how it is used before you start using it. Otherwise you will always need someone to write the code for you!
Output will be (in excel):
Here is a simplified version of what I think you are trying to do.
Example hostheaders.csv before code runs
www.google.com
www.stackoverflow.com
www.fakewebsitehere.com
Updated code
string filePath = #"c:\hostnames.csv";
List<string> results = new List<string>();
using (var reader = new StreamReader(filePath))
{
Ping ping = new Ping();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
var hostname = values[0];
Console.WriteLine(hostname);
PingReply pingresult = ping.Send(hostname);
results.Add($"{line},{pingresult.Status.ToString()}");
}
}
File.WriteAllLines(filePath, results);
hostheaders.csv after code runs
www.google.com,Success
www.stackoverflow.com,Success
www.fakewebsitehere.com,TimedOut
I'm trying to make a parser which will read a CSV file and add information in a List.
My problem is that when I make my loop to add in a Dictionary, the header(key) and the value(value), I have a error
"Impossible to cast void in List" (translated from French).
Code:
private List<string> header = null;
private List<string> tableauValeurs = null;
public bool ParseCSVFile(string csvPath)
{
bool result = false;
if (File.Exists(csvPath))
{
using (StreamReader sr = new StreamReader(csvPath))
{
var firsLine = sr.ReadLine();
this.header = firsLine.Split(';').ToList();
while (sr.Peek() >= 0)
{
var line = sr.ReadLine().Split(';');
this.tableauValeurs = new List<string>();
Dictionary<string, List<string>> lineDico = new Dictionary<string, List<string>>();
for (int i = 0; i < this.header.Count; i++)
{
lineDico.Add(this.header[i], ***this.tableauValeurs.Add(line[i]***);
}
}
result = true;
}
}
return result;
}
Any idea? Thanks
The method Add is a void method. It doesn't return a new list.
So, add your element into the list first, then put the list into the Dictionary.
this.tableauValeurs.Add(line[i]);
lineDico.Add(this.header[i], this.tableauValeurs);
Add returns void. You need to add your line to tableauValeurs first and then add tableauValeurs to your Dictionary
You have:
lineDico.Add(this.header[i], this.tableauValeurs.Add(line[i]));
Now; lineDico is Dictionary<string, List<string>>, so the Add method wants a string and a List<string>; this.header[i] looks fine, but this.tableauValeurs.Add(line[i]) will return void, which isn't a List<string>. So: what is the List<string> that you intended to add as the value in lineDico?
this.tableauValeurs.Add(line[i]) doesnt return a list.
Probably best to use: (no need of this if field is only in this class)
tableauValeurs.Add(line[i]));
lineDico.Add(header[i], tableauValeurs);
If you wish to have a new list associated with dictionary, move the new list declaration after for loop.
This is my code. Im using the function to retrieve the list but its not sending it.
public List<string> country_set()
{
mCountryUrl = new Uri ("http://xxxxxxx.wwww/restservice/country");
mList = new List<string> ();
mCountry = new List<Country> ();
WebClient client = new WebClient ();
client.DownloadDataAsync (mCountryUrl);
client.DownloadDataCompleted += (sender, e) => {
RunOnUiThread (() => {
string json = Encoding.UTF8.GetString (e.Result);
mCountry = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Country>> (json);
Console.WriteLine (mCountry.Count.ToString());
int x = mCountry.Count;
for(int i=0; i< x ; i++)
{
mList.Add(mCountry[i].name);
}
});
};
return mList;
}
It throws an exception .
Kindly help me out
The problem is that you return the mList immediately after your method completes which is before the call to the web server completes. Now after your calling code inspects the list to find it empty, eventually the call to the server will complete and your list will be filled which is too late!
This will fix the problem:
var mCountryUrl = new Uri("http://xxxxxxx.wwww/restservice/country");
var mList = new List<string>();
var mCountry = new List<Country>();
WebClient client = new WebClient();
var data = client.DownloadData(mCountryUrl);
string json = Encoding.UTF8.GetString(data);
mCountry = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Country>>(json);
Console.WriteLine(mCountry.Count.ToString());
int x = mCountry.Count;
for (int i = 0; i < x; i++)
{
mList.Add(mCountry[i].name);
}
return mList;
How about this one:
public async Task<List<string>> country_set()
{
mCountryUrl = new Uri ("http://xxxxxxx.wwww/restservice/country");
mList = new List<string>();
mCountry = new List<Country>();
WebClient client = new WebClient();
byte[] data = await client.DownloadDataTaskAsync(mCountryUrl);
string json = Encoding.UTF8.GetString(data);
mCountry = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Country>> (json);
Console.WriteLine (mCountry.Count.ToString());
int x = mCountry.Count;
for(int i=0; i<x; i++)
mList.Add(mCountry[i].name);
return mList;
}
It uses the new async model from .Net.
EDIT: Code is typed from the Android app. Anyone who spots syntax mistakes (or any other kind), please signal them in a comment.
I have a wcf ksoap2 service that returns Dictionary<ArrayList, List<byte[]>>. Now at android side I want to fill my Dictionary<String[], ArrayList<Object>> diction; from wcf response. I am new to wcf and android/java, I don't have idea how to do this. Please provide me some better example of filling Dictionary with wcf.
Thanks in advance
This is my wcf code
public Dictionary<ArrayList, List<byte[]>> getImages()
{
Dictionary<ArrayList, List<byte[]>> image_Name = new Dictionary<ArrayList, List<byte[]>>();
DirectoryInfo directoryInfo = new DirectoryInfo(#"C:\Users\Yakhtar\Desktop\abc");
arr1 = new ArrayList();
foreach (FileInfo fi in directoryInfo.GetFiles())
arr1.Add(fi.FullName);
list = new List<byte[]>();
for (int i = 0; i < arr1.Count; i++)
{
img = Image.FromFile(arr1[i].ToString());
ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
list.Add(ms.ToArray());
}
image_Name.Add(arr1, list);
//image_Name[arr1 as ArrayList] = [list as byte[]];
return image_Name;
}
Well I am not sure about that but have you thought about JSON parsing instead of ksoap2 ??
Here is a tutorial on how to work with array of complex objects with KSOAP. I found out by countless hours of debugging. Hope this hepls
also try this
SoapObject countryDetails = (SoapObject)envelope.getResponse();
System.out.println(countryDetails.toString());
ArrayList list = new ArrayList(countryDetails.getPropertyCount());
lv_arr = new String[countryDetails.getPropertyCount()];
for (int i = 0; i < countryDetails.getPropertyCount(); i++) {
Object property = countryDetails.getProperty(i);
if (property instanceof SoapObject) {
SoapObject countryObj = (SoapObject) property;
String countryName = countryObj.getProperty("countryName").toString();
list.add(countryName );
}
}
Do something like this..
list = new List<byte[]>();
for (int i = 0; i < arr1.Count; i++)
{
img = Image.FromFile(arr1[i].ToString());
ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
list.Add(ms.ToArray());
}
image_Name.Add(arr1, list);
//image_Name[arr1 as ArrayList] = [list as byte[]];
return image_Name;
}
I have a webapp that goes and makes some webgets and returns the results in Gridview. Sometimes, the app may need to make 400+ webgets, but only populate the grid with 15-20 records.
Is there a way to partially load a GridView, so that each record is appended to the existing GridView?
Adding Code
List<Test> testList = new List<Test>();
foreach (Location str in list)
{
string url;
try
{
url = "http://www.test.com/" + str.Url;
XmlReader reader = XmlReader.Create(url);
Rss10FeedFormatter formatter = new Rss10FeedFormatter();
if (formatter.CanRead(reader))
{
formatter.ReadFrom(reader);
IEnumerable<SyndicationItem> items = formatter.Feed.Items;
int itemCt = 1;
foreach (SyndicationItem item in items)
{
Test test = new Test();
test.Name = item.Name;
test.City= item.City;
testList.Add(data);
//if I add this here, the RowDatabound does not fire, if I take out, it works fine but only after all requests are made
listGrid.DataSource = temp;
listGrid.DataBind();
}
}
else
throw new ApplicationException("Invalid RSS 1.0 feed at " + FeedUrl.Text.Trim());
}
Create a separate list that you will DataBind the gridview to, and then whenever you change the elements in that list just rebind the gridview.
var mySmallerList = bigList.Skip(someNumber).Take(someOtherNumber);
myGridView.DataSource = mySmallerList;