Error 500 Trying to Connect to NetSuite RESTlet using C# - 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.

Related

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.

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.

Changing HTTP Request to HTTPS, message content seems to be lost

This is not a subject I am strong in so I apologize ahead of time if I say something ridiculous.
I have developed an HTTP service using Mule. I have it functioning perfectly when I connect directly to the service and send data using a test harness I wrote in C#.
As the final part of my testing, I need to send it to an HTTPS URL that is supposed to "decrypt" the message and forward it to my service. When I send a message to the HTTPS URL, it gets forwarded to my service but the message contents appear empty and therefore does not get processed. I understand that I may have to add some "encryption" to my Test Harness but I have been researching how to do this all day and nothing I have found is answering my question.
Here is an example of the code I am using for the simple HTTP request:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(ConfigurationManager.AppSettings["HttpDestination"].ToString());
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = data.Length;
using (Stream strm = req.GetRequestStream())
{
strm.Write(data, 0, data.Length);
}
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
What do I need to change here to make this work?
Here is the solution that I discovered here. I needed to add the following line:
req.ProtocolVersion = System.Net.HttpVersion.Version10;
Without this, a timeout was occurring when getting the request stream and the content was never being sent, only the headers.

Call to REST web service results in response of error 503 but not with a tool like Poster

I am trying to send an xml document to a REST web service. Using a tool like Poster, the call works fine (With the xml in the body of the "content"), but in my code, I get error 503 server unavailable.
Link to Poster: https://addons.mozilla.org/en-US/firefox/addon/poster/
My code is as follows (this is a test-harness so no error handling etc):
string s = "";
using (StreamReader sr = new StreamReader(#"c:\users\dev.admin\documents\visual studio 2010\Projects\WindowsFormsApplication2\WindowsFormsApplication2\XMLFile1.xml"))
{
s = sr.ReadToEnd();
}
string url = FULL_URL_WITH_PARAMETERS; // SAME URL AS USED IN POSTER
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
request.ContentType = "text/xml;charset=UTF-8";
string data = s;
Stream postStream = null;
using (StreamWriter requestStream = new StreamWriter(request.GetRequestStream()))
{
requestStream.Write(data);
}
HttpWebResponse pervasiveResponse = (HttpWebResponse)request.GetResponse();
StreamReader sr1 = new StreamReader(pervasiveResponse.GetResponseStream(), System.Text.Encoding.Default);
string backstr = sr1.ReadToEnd();
}
else
{
throw new ArgumentNullException();
}
REST Starter Kit? WCF Web API (Glenn Block's project, now part of AppFabric int he 4.5 time frame? (although I believe there will be a standalone))? Roll your own joint?
Regardless, I think working with binary is the correct method to go. For that reason, I would start with something like this article. Not because I tried the code and think it is a great article, but merely because the article shows sending XML and I don't have time to find the client I wrote a few months ago. ;-)
If I can break free long enough to find my code, I will post a sample.
Oh, another tool you should look at is SoapUI. No, it is not just for testing SOAP, as it supports REST (and other methodologies) as well as SOAP. One nice benefit of using SoapUI is the ability to move the tests over to LoadUI and stress testing a service. If you are merely consuming services, it might not have the greatest value, of course.

Can I send an empty HTTP POST WebRequest object from C# to IIS?

Do I need to just slap some random garbage data in a WebRequest object to get by the HTTP status code 411 restriction on IIS?
I have an HttpPost action method in an MVC 3 app that consumes a POST request with all the relevant information passed in the querystring (no body needed).
[HttpPost] public ActionResult SignUp(string email) { ... }
It worked great from Visual Studio's built in web host, Cassini. Unfortunately, once the MVC code was live on IIS [7.5 on 2008 R2], the server is pitching back an HTTP error code when I hit it from my outside C# form app.
The remote server returned an error:
(411) Length Required.
Here is the calling code:
WebRequest request = WebRequest.Create("http://somewhere.com/signup/?email=a#b.com");
request.Method = "POST";
using (WebResponse response = request.GetResponse())
using (Stream responseStream = response.GetResponseStream())
using (StreamReader responseReader = new StreamReader(responseStream)) {
// Do something with responseReader.ReadToEnd();
}
Turns out you can get this to go through by simply slapping an empty content length on the request before you send it.
WebRequest request = WebRequest.Create("http://somewhere.com/signup/?email=a#b.com");
request.Method = "POST";
request.ContentLength = 0;
Not sure how explicitly giving an empty length vs. implying one makes a difference, but IIS was happy after I did. There are probably other ways around this, but this seems simple enough.
I believe you are required to set a Content-Length header anytime you post a request to a web server:
http://msdn.microsoft.com/en-us/library/system.web.httprequest.contentlength.aspx
You could try a GET request to test it.

Categories