Getting bad request (400) on solr - c#

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.

Related

C# HttpWebRequest does not work with my URL in MAUI app while URL works without any problem in browser directly or via PHP

When I try to load data from the URL I get:
System.Net.WebException: 'The remote server returned an error: (401) Unauthorized.'
My Code in MAUI app:
var url = "http://somedomen.com/GetDataCSV.php?GetDataCSV=&type=xml";
WebRequest request = HttpWebRequest.Create(url);
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string responseText = reader.ReadToEnd();
The URL works very well in PHP:
$products = file_get_contents("http://somedomen.com/GetDataCSV.php?GetDataCSV=&type=xml");
$xml = simplexml_load_string($products);
It works very well in a browser directly without any authorization.
What I am doing wrang and how to solve the problem?

Error 500 Trying to Connect to NetSuite RESTlet using C#

I'm trying to build a connector between a credit card processor and a client's NetSuite account to help automate their reconciliation process. Not having any trouble pulling info from their card processor, except occasionally when it has its own error 500. Still trying to find a way to better handle it than what I'm doing.
Anyway, the problem is occurring when I try to connect to the RESTlet in their NetSuite account. I can't seem to get it to properly connect, and all I get are error 500 responses. The problem doesn't seem to be the RESTlet as I can communicate with it fine when using tools like "Send HTTP Tool" to test it out. This is the code I'm using currently:
JavaScriptSerializer jss = new JavaScriptSerializer();
String content = jss.Serialize(rd);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(this.nsURL);
req.ContentType = "application/json";
req.ContentLength = content.Length;
req.Method = "POST";
WebHeaderCollection headers = new WebHeaderCollection();
headers.Add("User-Agent-X", "SuiteScript-Call");
headers.Add("Authorization", String.Format("NLAuth nlauth_account={0},nlauth_email={1},nlauth_signature={2},nlauth_role={3}",
this.nsAccount, this.nsEmail, this.nsPassword, this.nsRole));
req.Headers = headers;
using (StreamWriter requestWriter = new StreamWriter(req.GetRequestStream()))
{
requestWriter.Write(content);
}
HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); // error occurs here
Stream stream = resp.GetResponseStream();
using (StreamReader reader = new StreamReader(stream))
{
String response = reader.ReadToEnd();
Console.WriteLine(response);
}
And this is the error recorded:
[System.Net.WebException] The remote server returned an error: (500) Internal Server Error.
The error occurs at the point I try to retrieve the response from the HttpWebRequest object. I have tried sending the data instead as a byte array, but that causes a different error at the point I actually write the data to the connection. The RESTlet is also setup to log the data it receives upon being called, but this error is occurring before the RESTlet function can even be triggered.
I've spent too much time trying various different "fixes" to get this to work with no luck.
---- EDIT ----
Forgot to mention that I'm using Visual Studio 2010 Pro and targeting .NET 4.

wowza streaming c# HTTP get method for livestreamrecord

Hello I need some help with livestreamrecord recording via http url c# calls,
I can start and stop a stream recording using:
http://[username]:[password]#[wowza-ip-address]:8086/livestreamrecord?app=live&streamname=myStream&action=startRecording
When inputting it directly in to a browser or when I redirect my webpage to it from code, but when I try to do the same from c# on a webpage nothing happens.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://[username]:[password]#[wowza-ip-address]:8086/livestreamrecord?app=live&streamname=myStream&action=startRecording);
request.Method = "GET";
request.Proxy = new WebProxy("[wowza-ip-address]", 8086);
request.Credentials = new NetworkCredential("[username]", "[password]");
request.ContentType = "application/x-www-form-urlencoded";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stm = response.GetResponseStream();
When I first tried to set it up I kept getting 401 unauthorized error which the code above no long throws, but now I'm stuck as no error is thrown and still no recording is started.
The response returns:
<html><head><title>Wowza Streaming Engine 4 Perpetual Edition 4.0.1 build10615</title></head><body>Wowza Streaming Engine 4 Perpetual Edition 4.0.1 build10615</body></html>
Which indicates it is not reaching the livestreamrecord page.
Ben
Sovled the problem, thier was a 302 redirect on the request which was producing the 401 unauthorized responses. Adding:
request.AllowAutoRedirect = false;
and removing:
request.Proxy = new WebProxy("[wowza-ip-address]", 8086);
Resolved the problem.
With Proxy in, it didn't throw an error but also didn't record. Removing Proxy allowed the record command to work but throw the 401 response which was then resolved by setting AllowAutoRedirect to false.

Download google location history using http request

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!

HttpWebRequest returns empty text

When I try to fetch the content of this URL
http://www.yellowpages.com.au/qld/gatton/a-12000029-listing.html
using System.Net;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(link);
request.AllowAutoRedirect = true;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();
StreamReader objSR;
objSR = new StreamReader(resStream, System.Text.Encoding.GetEncoding("utf-8"));
string sResponse = objSR.ReadToEnd();
I don't get any response from the server. Please help me find out why this happens.
Thanks in advance!
It may well be looking at the user agent and refusing to serve content to a client that doesn't identify itself. Try setting the UserAgent property on your request object.
Looks to me like that site is checking the referrer url and may be serving up empty content if an invalid referrer is specified.
Try setting request.Referer = "http://www.google.com";. Experiment with the referrer to see if that changes the response. I'd also try the UserAgent property as Matthew suggested.
I had the same problem and the cause was that I previously had set the method to HEAD and in later revisions had the need to parse the body.

Categories