Download google location history using http request - c#

I'm trying to write a c# code that downloads the kml file from https://maps.google.com/locationhistory/b/0
I wrote the following code, but the http response I get back brings me to the login page even though
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://maps.google.com/locationhistory/b/0/kml?startTime=1373666400000&endTime=1373752800000");
request.Credentials = new NetworkCredential("username", "password");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Get the stream associated with the response.
Stream receiveStream = response.GetResponseStream();
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
using (StreamWriter outfile = new StreamWriter(#"temp.kml"))
{
outfile.Write(readStream.ReadToEnd());
}
response.Close();
readStream.Close();
I try to pass the user credentials.
I already tried these solutions :
- How to use HttpWebRequest.Credentials Property for Basic Authentication?
- C# HttpWebRequest using Basic authentication
but nothing worked.
Does anybody knows how to pass the google credentials, and has experienced google location history api?
Thanks!

Related

Upload to FTP, not using anonymous C#

I used the following example code to upload to an FTP, it turns out that the server don't want to use anonymous connection, but I can't figure out how to change it to be able to upload with out breaking that rule.
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/" + fileName);
request.Method = WebRequestMethods.Ftp.UploadFile;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential("username", "password");
// Copy the contents of the file to the request stream.
byte[] fileContents;
using (StreamReader sourceStream = new StreamReader(file))
{
fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
}
request.ContentLength = fileContents.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(fileContents, 0, fileContents.Length);
}
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
}
Anonymous credentials are still credentials. You can get rid of them by removing the line
request.Credentials = new NetworkCredential("username", "password");
and you won't be sending any credentials to the server at all.
Nevertheless, as mentioned in "Remarks" section of the MSDN docs, FtpWebRequest class is not recommended to be used for new development. Microsoft recommends to use one of third-party libraries listed here.
I also recommend choosing one of those, as they provide more robust way to communicate with FTP servers. You can enable logging and it will be easier for you to see why exactly your communication with the server is failing. You will see all the commands your app is sending to the server and also all the responses from the server. These libraries also implement all the basic operations like listing, downloading, uploading, permission setting, sync/async etc, so you don't have to write them yourself.

Getting bad request (400) on solr

I am facing a weird problem with solr.
I am getting bad request(400) error when requesting a url with HttpWebRequest(C#)
We are using below code
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(solrFinalUrl);
HttpWebResponse response = ttpWebResponse)request.GetResponse();
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream,Encoding.UTF8);
String jsonString = readStream.ReadToEnd();
But when I am hitting the url on browser it's giving expected result.
Request Url --
http://__________:8080/solr-4.10.3/lemars/select?start=0&rows=10&q=*%3A*&fq=%20(date:[1887-08-01T00:00:00Z%20TO%201887-08-31T23:59:59Z])%20(date:[1888-08-01T00:00:00Z%20TO%201888-08-31T23:59:59Z])%20(date:[1889-08-01T00:00:00Z%20TO%201889-08-31T23:59:59Z])%20(date:[1890-08-01T00:00:00Z%20TO%201890-08-31T23:59:59Z])%20(date:[1891-08-01T00:00:00Z%20TO%201891-08-31T23:59:59Z])%20(date:[1892-08-01T00:00:00Z%20TO%201892-08-31T23:59:59Z])%20(date:[1893-08-01T00:00:00Z%20TO%201893-08-31T23:59:59Z])%20(date:[1894-08-01T00:00:00Z%20TO%201894-08-31T23:59:59Z])%20(date:[1895-08-01T00:00:00Z%20TO%201895-08-31T23:59:59Z])%20(date:[1896-08-01T00:00:00Z%20TO%201896-08-31T23:59:59Z])%20(date:[1897-08-01T00:00:00Z%20TO%201897-08-31T23:59:59Z])%20(date:[1898-08-01T00:00:00Z%20TO%201898-08-31T23:59:59Z])%20(date:[1899-08-01T00:00:00Z%20TO%201899-08-31T23:59:59Z])%20(date:[1900-08-01T00:00:00Z%20TO%201900-08-31T23:59:59Z])%20(date:[1901-08-01T00:00:00Z%20TO%201901-08-31T23:59:59Z])%20(date:[1902-08-01T00:00:00Z%20TO%201902-08-31T23:59:59Z])%20(date:[1903-08-01T00:00:00Z%20TO%201903-08-31T23:59:59Z])%20(date:[1904-08-01T00:00:00Z%20TO%201904-08-31T23:59:59Z])%20(date:[1905-08-01T00:00:00Z%20TO%201905-08-31T23:59:59Z])%20(date:[1906-08-01T00:00:00Z%20TO%201906-08-31T23:59:59Z])%20(date:[1907-08-01T00:00:00Z%20TO%201907-08-31T23:59:59Z])%20(date:[1908-08-01T00:00:00Z%20TO%201908-08-31T23:59:59Z])%20(date:[1909-08-01T00:00:00Z%20TO%201909-08-31T23:59:59Z])%20(date:[1910-08-01T00:00:00Z%20TO%201910-08-31T23:59:59Z])%20(date:[1911-08-01T00:00:00Z%20TO%201911-08-31T23:59:59Z])%20(date:[1912-08-01T00:00:00Z%20TO%201912-08-31T23:59:59Z])%20(date:[1913-08-01T00:00:00Z%20TO%201913-08-31T23:59:59Z])%20(date:[1914-08-01T00:00:00Z%20TO%201914-08-31T23:59:59Z])%20(date:[1915-08-01T00:00:00Z%20TO%201915-08-31T23:59:59Z])%20(date:[1916-08-01T00:00:00Z%20TO%201916-08-31T23:59:59Z])%20(date:[1917-08-01T00:00:00Z%20TO%201917-08-31T23:59:59Z])%20(date:[1918-08-01T00:00:00Z%20TO%201918-08-31T23:59:59Z])%20(date:[1919-08-01T00:00:00Z%20TO%201919-08-31T23:59:59Z])%20(date:[1920-08-01T00:00:00Z%20TO%201920-08-31T23:59:59Z])%20(date:[1921-08-01T00:00:00Z%20TO%201921-08-31T23:59:59Z])%20(date:[1922-08-01T00:00:00Z%20TO%201922-08-31T23:59:59Z])%20(date:[1923-08-01T00:00:00Z%20TO%201923-08-31T23:59:59Z])%20(date:[1924-08-01T00:00:00Z%20TO%201924-08-31T23:59:59Z])%20(date:[1925-08-01T00:00:00Z%20TO%201925-08-31T23:59:59Z])%20(date:[1926-08-01T00:00:00Z%20TO%201926-08-31T23:59:59Z])%20(date:[1927-08-01T00:00:00Z%20TO%201927-08-31T23:59:59Z])%20(date:[1928-08-01T00:00:00Z%20TO%201928-08-31T23:59:59Z])%20(date:[1929-08-01T00:00:00Z%20TO%201929-08-31T23:59:59Z])%20(date:[1930-08-01T00:00:00Z%20TO%201930-08-31T23:59:59Z])%20(date:[1931-08-01T00:00:00Z%20TO%201931-08-31T23:59:59Z])%20(date:[1932-08-01T00:00:00Z%20TO%201932-08-31T23:59:59Z])%20(date:[1933-08-01T00:00:00Z%20TO%201933-08-31T23:59:59Z])%20(date:[1934-08-01T00:00:00Z%20TO%201934-08-31T23:59:59Z])%20(date:[1935-08-01T00:00:00Z%20TO%201935-08-31T23:59:59Z])%20(date:[1936-08-01T00:00:00Z%20TO%201936-08-31T23:59:59Z])%20(date:[1937-08-01T00:00:00Z%20TO%201937-08-31T23:59:59Z])%20(date:[1938-08-01T00:00:00Z%20TO%201938-08-31T23:59:59Z])%20(date:[1939-08-01T00:00:00Z%20TO%201939-08-31T23:59:59Z])%20(date:[1940-08-01T00:00:00Z%20TO%201940-08-31T23:59:59Z])%20(date:[1941-08-01T00:00:00Z%20TO%201941-08-31T23:59:59Z])%20(date:[1942-08-01T00:00:00Z%20TO%201942-08-31T23:59:59Z])%20(date:[1943-08-01T00:00:00Z%20TO%201943-08-31T23:59:59Z])%20(date:[1944-08-01T00:00:00Z%20TO%201944-08-31T23:59:59Z])%20(date:[1945-08-01T00:00:00Z%20TO%201945-08-31T23:59:59Z])%20(date:[1946-08-01T00:00:00Z%20TO%201946-08-31T23:59:59Z])%20(date:[1947-08-01T00:00:00Z%20TO%201947-08-31T23:59:59Z])%20(date:[1948-08-01T00:00:00Z%20TO%201948-08-31T23:59:59Z])%20(date:[1949-08-01T00:00:00Z%20TO%201949-08-31T23:59:59Z])%20(date:[1950-08-01T00:00:00Z%20TO%201950-08-31T23:59:59Z])%20(date:[1951-08-01T00:00:00Z%20TO%201951-08-31T23:59:59Z])%20(date:[1952-08-01T00:00:00Z%20TO%201952-08-31T23:59:59Z])%20(date:[1953-08-01T00:00:00Z%20TO%201953-08-31T23:59:59Z])%20(date:[1954-08-01T00:00:00Z%20TO%201954-08-31T23:59:59Z])%20(date:[1955-08-01T00:00:00Z%20TO%201955-08-31T23:59:59Z])%20(date:[1956-08-01T00:00:00Z%20TO%201956-08-31T23:59:59Z])%20(date:[1957-08-01T00:00:00Z%20TO%201957-08-31T23:59:59Z])%20(date:[1958-08-01T00:00:00Z%20TO%201958-08-31T23:59:59Z])%20(date:[1959-08-01T00:00:00Z%20TO%201959-08-31T23:59:59Z])%20(date:[1960-08-01T00:00:00Z%20TO%201960-08-31T23:59:59Z])%20(date:[1961-08-01T00:00:00Z%20TO%201961-08-31T23:59:59Z])%20(date:[1962-08-01T00:00:00Z%20TO%201962-08-31T23:59:59Z])%20(date:[1963-08-01T00:00:00Z%20TO%201963-08-31T23:59:59Z])%20(date:[1964-08-01T00:00:00Z%20TO%201964-08-31T23:59:59Z])%20(date:[1966-08-01T00:00:00Z%20TO%201966-08-31T23:59:59Z])%20(date:[1967-08-01T00:00:00Z%20TO%201967-08-31T23:59:59Z])%20(date:[1968-08-01T00:00:00Z%20TO%201968-08-31T23:59:59Z])%20(date:[1969-08-01T00:00:00Z%20TO%201969-08-31T23:59:59Z])%20(date:[1970-08-01T00:00:00Z%20TO%201970-08-31T23:59:59Z])%20(date:[1971-08-01T00:00:00Z%20TO%201971-08-31T23:59:59Z])%20(date:[1972-08-01T00:00:00Z%20TO%201972-08-31T23:59:59Z])%20(date:[1973-08-01T00:00:00Z%20TO%201973-08-31T23:59:59Z])%20(date:[1974-08-01T00:00:00Z%20TO%201974-08-31T23:59:59Z])%20(date:[1975-08-01T00:00:00Z%20TO%201975-08-31T23:59:59Z])%20(date:[1976-08-01T00:00:00Z%20TO%201976-08-31T23:59:59Z])%20(date:[1977-08-01T00:00:00Z%20TO%201977-08-31T23:59:59Z])%20(date:[1978-08-01T00:00:00Z%20TO%201978-08-31T23:59:59Z])%20(date:[1979-08-01T00:00:00Z%20TO%201979-08-31T23:59:59Z])%20(date:[1980-08-01T00:00:00Z%20TO%201980-08-31T23:59:59Z])%20(date:[1981-08-01T00:00:00Z%20TO%201981-08-31T23:59:59Z])%20(date:[1982-08-01T00:00:00Z%20TO%201982-08-31T23:59:59Z])%20(date:[1983-08-01T00:00:00Z%20TO%201983-08-31T23:59:59Z])%20(date:[1984-08-01T00:00:00Z%20TO%201984-08-31T23:59:59Z])%20(date:[1985-08-01T00:00:00Z%20TO%201985-08-31T23:59:59Z])%20(date:[1986-08-01T00:00:00Z%20TO%201986-08-31T23:59:59Z])%20(date:[1987-08-01T00:00:00Z%20TO%201987-08-31T23:59:59Z])%20(date:[1988-08-01T00:00:00Z%20TO%201988-08-31T23:59:59Z])%20(date:[1989-08-01T00:00:00Z%20TO%201989-08-31T23:59:59Z])%20(date:[1990-08-01T00:00:00Z%20TO%201990-08-31T23:59:59Z])%20(date:[1991-08-01T00:00:00Z%20TO%201991-08-31T23:59:59Z])%20(date:[1992-08-01T00:00:00Z%20TO%201992-08-31T23:59:59Z])%20(date:[1993-08-01T00:00:00Z%20TO%201993-08-31T23:59:59Z])%20(date:[1994-08-01T00:00:00Z%20TO%201994-08-31T23:59:59Z])%20(date:[1995-08-01T00:00:00Z%20TO%201995-08-31T23:59:59Z])%20(date:[1996-08-01T00:00:00Z%20TO%201996-08-31T23:59:59Z])%20(date:[1997-08-01T00:00:00Z%20TO%201997-08-31T23:59:59Z])%20(date:[1998-08-01T00:00:00Z%20TO%201998-08-31T23:59:59Z])%20(date:[1999-08-01T00:00:00Z%20TO%201999-08-31T23:59:59Z])%20(date:[2000-08-01T00:00:00Z%20TO%202000-08-31T23:59:59Z])%20(date:[2001-08-01T00:00:00Z%20TO%202001-08-31T23:59:59Z])%20(date:[2002-08-01T00:00:00Z%20TO%202002-08-31T23:59:59Z])%20(date:[2003-08-01T00:00:00Z%20TO%202003-08-31T23:59:59Z])%20(date:[2004-08-01T00:00:00Z%20TO%202004-08-31T23:59:59Z])%20(date:[2005-08-01T00:00:00Z%20TO%202005-08-31T23:59:59Z])%20(date:[2006-08-01T00:00:00Z%20TO%202006-08-31T23:59:59Z])%20(date:[2007-08-01T00:00:00Z%20TO%202007-08-31T23:59:59Z])%20(date:[2008-08-01T00:00:00Z%20TO%202008-08-31T23:59:59Z])%20(date:[2009-08-01T00:00:00Z%20TO%202009-08-31T23:59:59Z])%20(date:[2010-08-01T00:00:00Z%20TO%202010-08-31T23:59:59Z])%20(date:[2011-08-01T00:00:00Z%20TO%202011-08-31T23:59:59Z])%20(date:[2012-08-01T00:00:00Z%20TO%202012-08-31T23:59:59Z])&fl=filename%2Ctitle%2CfileContent%2Cdate%2Ccity%2Cstate%2Cpagenum&wt=json&indent=true&facet=true&facet.range=date&facet.range.start=1887-01-01T00:00:00Z/MONTH&facet.range.end=2012-12-31T23:59:59Z/MONTH%2B1MONTH&facet.range.gap=%2B1DAY
Please help.
You can analyze http packages by wireshark. Compare if there are any different between connect from c# application and browser.

how to upload a pdf file and other string parameter in rest wcf service

I had created a rest wcf service and now i want to post the pdf file and some other string data to it so what is the Correct way to do it
1) converting the pdf to base64 string and then form the xml and post it to rest web service decode it in the rest service.
2) directly uploading using the below code
byte[] pdfFile = File.ReadAllBytes("pdf file path here");WebRequest request = WebRequest.Create("https://test.site.fr/Testfile");
request.Method = "POST";
request.ContentLength = pdfFile.Length;
request.ContentType = "application/pdf";
Stream stream = request.GetRequestStream();
stream.Write(pdfFile, 0, pdfFile.Length);
stream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
Console.WriteLine(reader.ReadToEnd());
reader.Close();
which is better with respect to large files and if any other compression required
The best way to do this is to use multipart/form-data. C# does not have an inbuilt multipart/form-data parser so use this parser written by one of the developers from the C# community.
HttP Multipart Data Parser

Accessing and getting response from API call

I am checking out the namecheap api and am having some difficulty getting started. I am trying to access the api after setting up the sandbox account etc and a sample response is in XML format:
<ApiResponse Status="OK" xmlns="http://api.namecheap.com/xml.response">
<Errors />
<Warnings />
<RequestedCommand>namecheap.domains.check</RequestedCommand>
<CommandResponse>
<DomainCheckResult Domain="google.com" Available="false" />
</CommandResponse>
<Server>WEB1-SANDBOX1</Server>
<GMTTimeDifference>--4:00</GMTTimeDifference>
<ExecutionTime>0.875</ExecutionTime>
</ApiResponse>
I know how to parse XML, but what I need a little guidance with is how do I get started with the actual request/response part of the API call.
I know which parameters I need to send, and I know I need the api key and url, but how do I write the WebRequest and WebResponse part of it? Or can Linq provide me a way to achieve that too?
I was trying to use:
WebRequest req = HttpWebRequest.Create(url + apikey + username + command + domain);
WebResponse response = req.GetResponse();
But I don't see a way to do anything with variable response.
How can I make a very simple API call to this API and get its response into XML format so I can parse it?
Any help at all is really appreciated.
You need to get the Response Stream associated and read from it:
// Get the stream associated with the response.
Stream receiveStream = response.GetResponseStream ();
StreamReader readStream = new StreamReader (receiveStream, Encoding.UTF8);
Console.WriteLine ("Response stream received.");
Console.WriteLine (readStream.ReadToEnd ());
WebResponse is abstract, you must cast it to an HttpWebResponse.
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
You will then be able to access the various information inside that you're looking for.
Also, you might consider using a WebClient if you're just doing simple web requests, it's much easier to work with... literally as easy as:
string response = new WebClient().DownloadString("http://somewebsite.com/some/resource?params=123");

Communicating with an ASP website

I have a know a website that contains an open database of the result of an academic test.
http://nts.org.pk/NTSWeb/PPL_30Sep2012_Result/search.asp
I am expert with C# but newbie to web development.
Usually, using web browser, we can enter and roll number and server sends back the result. E.G. Use my Roll Num: 3912125
What I need to do is, use a C# application to communicate this roll null number and get anything, of my result. (any string is excepted, I will parse out my result from that string.)
How do I send query? when I don't know a list of possible query strings.
I tried this code:
string queryString = "RollNo=3912125";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(#"http://nts.org.pk/NTSWeb/PPL_30Sep2012_Result/search.asp");
request.UseDefaultCredentials = true;
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
byte[] requestBytes = Encoding.UTF8.GetBytes(queryString);
request.ContentLength = requestBytes.Length;
using (var requestStream = request.GetRequestStream())
{
requestStream.Write(requestBytes, 0, requestBytes.Length);
requestStream.Close();
}
WebResponse response = request.GetResponse();
textBox1.AppendText(((HttpWebResponse)response).StatusDescription);
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
textBox1.AppendText(responseFromServer);
reader.Close();
dataStream.Close();
response.Close();
You have to append the querystring to the url like this:
string queryString = "RollNo=3912125";
string url = String.Format(#"http://foo/search.asp?{0}", queryString);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
You should take a look at the code in my answer to C# https login and download file. It gives a good demonstration of how to perform a POST request. As far as knowing what's valid to use for the query-formatted string in your POST, it's simply a matter of looking for appropriate input elements in the page content. It looks like what you have (RollNo) is correct. You may, however, need to also add the submit button value to your request as well depending on how the server behaves, giving you something like. RollNo=3912125&submit=submit.
You're most of the way there. Your queryString should look like RollNo=3912125&Submit=+Search+. When you are calling WebRequest.Create, the Url should in fact be http://nts.org.pk/NTSWeb/PPL_30Sep2012_Result/result.asp.
The rest of your code should work, though the answer #JamieSee recommended to you has some very good advice about wrapping things in using blocks correctly

Categories