API woes with .Net in C# - c#

I am new to .net and APIs and am currently using .Net 4.5 to connect to an API using rest. The problem I am having is I get an exception thrown back in the return xml that says "Cannot forward request to server with name", "Cannot read data from connection", Connection reset", full error below.
What is odd is this script works fine on smaller datasets but when the response is large enough, I get that exception from the server thrown back. What has helped setting the keep alive to true, using httpversion10, and specifying gzip and sendchunked. I am using advanced rest client to test the server in chrome addins and it returns data fine on there with these larger dataset. It will not with the script below. I am suspecting there is a difference in the way I am telling the server to handle my response verses the chrome add in. Any suggestions on how I improve the performance of this?
This is what the advanced rest client settings look like that work for the Chrome add in.
This is the code I have which appears to need changes to make it handle the request/response better.
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(#"magicalwebsite");
req.KeepAlive = true;
req.ProtocolVersion = HttpVersion.Version10;
req.ServicePoint.ConnectionLimit = 24;
req.Timeout = 2000000000;
req.Method = "Post";
req.Accept = "*/*";
req.SendChunked = true;
req.AutomaticDecompression = DecompressionMethods.GZip;
//Xml request file for data
string postData = System.IO.File.ReadAllText(#"C:\Users\yo\Desktop\testtest.txt");
//sending header and content
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
req.ContentType = "text/xml";
req.ContentLength = byteArray.Length;
req.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("xxxx:xxxxx"));
Stream dataStream = req.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
//Requesting response of data
HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
//Grabbing response
using (Stream stream = resp.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
String responseString = reader.ReadToEnd();
}
This is the exception I am getting in the xml being thrown back.
<?xml version="1.0" encoding="UTF-8"?>
<response success="false">
<messages>
<message key="exception-caught">Caught Exception: Caught Exception:
Cannot forward request to server with name=prod-euapp01
com.magicalpony.exception.APException: Cannot forward request to server with name=prod-euapp01
at com.magicalpony.webservices.APIForwarder.forward(APIForwarder.java:105)
at com.magicalpony.webservices.APIServlet.forwardRequest(APIServlet.java:270)
at com.magicalpony.webservices.APIServlet.wrongServer(APIServlet.java:253)
at com.magicalpony.webservices.APIServlet.service(APIServlet.java:124)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter
(ApplicationFilterChain.java:210)
at com.magicalpony.system.WebServiceMonitor.doFilter(WebServiceMonitor.java:61)
at org.apache.catalina.core.
ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at com.magicalpony.system.HitTracer.doFilter(HitTracer.java:133)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
at org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:303)
at org.apache.coyote.ajp.AjpProtocol$AjpConnectionHandler.process(AjpProtocol.java:183)
at org.apache.coyote.ajp.AjpProtocol$AjpConnectionHandler.process(AjpProtocol.java:169)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: com.magicalpony.exception.APException:
Cannot read data from connection
at com.magicalpony.webservices.NetUtil.readData(NetUtil.java:61)
at com.magicalpony.webservices.APIForwarder.forward(APIForwarder.java:102)
... 26 more
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:196)
at
java.net.SocketInputStream.read(SocketInputStream.java:122)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:275)
at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:687)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:633)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1323)
at com.magicalpony.webservices.NetUtil.readData(NetUtil.java:58)
... 27 more</message>
</messages>
</response>

The problem is with DNS resolution.
Step 1: Enter your domain name in a browser and see if Server is available.
Step 2: If server is available with domain name then you got to fix the IP Address or DNS resolution.
You can fix this by updating the IP Address in your PC (Follow steps below)
Go to a folder: C:\Windows\System32\drivers\etc
Copy and paste "hosts" file to desktop.
Update your host file with your IP Address and domain name.
Step 3: Copy and Paste hosts file in original folder (C:\Windows\System32\drivers\etc).
Step 4: Test your API.

Related

GovTalk / HMRC Transaction Engine Http 1.1 POST error

I appreciate this is a little bit niche, but I thought I would ask anyway. I'm writing a small c# application to utilise the HMRC web portal and electronically submit VAT returns in XML format. According to the HMRC specification it is just a simple Http 1.1 POST action required, and retrieving the response in XML. The application is built, however, I am having trouble with this code. I get an "OK" HttpWebResponse, but the HMRC server returns this spurious error message which they can't seem to tell me what it means. Here is my code:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl);
byte[] bytes;
bytes = System.Text.Encoding.ASCII.GetBytes(requestXml);
request.ContentType = "text/xml; charset='utf-8'";
request.ContentLength = bytes.Length;
request.Method = "POST";
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = response.GetResponseStream();
string responseStr = new StreamReader(responseStream).ReadToEnd();
return responseStr;
}
And the error is:
1001 - The submitted XML document either failed to validate against the GovTalk schema for this class of document or its body was badly formed.
I know the XML is ok, because when I test it using a third-party tool like "Postman" it submits 100% and the Transaction Engine returns no errors, so it must be my code. Does anything look glaringly wrong to post an XML? I have tried different Content/MIME Types and also I have confirmed that 'utf-8' is the correct encoding.
I was just wondering if any developers out there had worked on the Transaction Engine and could share their submit/post code?
The answer is that the code converting the file contents into the Byte Array was actually converting the file name.
I changed this line:
bytes = System.Text.Encoding.ASCII.GetBytes(requestXml);
To this:
bytes = File.ReadAllBytes(requestXml);

WCF stops responding after some requests

I have built wcf. it is working well
The issue is when I call it many times it displays the following error:
The server encountered an error processing the request. See server
logs for more details
I configured a WCF Tracing File but it remains always empty. what can be the reason of this sudden stop of the service and how to fix it?
Here is the code that I use at the client's side every 20 seconds:
string url = "http://host/Service.svc/method";
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
webrequest.Method = "GET";
ASCIIEncoding encoding = new ASCIIEncoding();
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
StreamReader loResponseStream =
new StreamReader(webresponse.GetResponseStream(), enc);
string strResult = loResponseStream.ReadToEnd();
loResponseStream.Close();
webresponse.Close();
I fixed the issue. it was due to open database connections. I missed to close, at the server side, the database connections. Thank you for answer
It could be a working memory issue on the server/host. If there's less than 5% available you get no response.

The underlying connection was closed: An unexpected error occurred

I have a very simple service which calls to a URL and captures a status that is written out by that service;
// Service call used to determine availability
System.Net.WebClient client = new System.Net.WebClient();
// I need this one (sorry, cannot disclose the actual URL)
Console.WriteLine(client.DownloadString(myServiceURL + ";ping"));
// I added this for test purposes
Console.WriteLine(client.DownloadString("https://www.google.com"));
The "DownloadString" for myServiceURL line throws the error "The underlying connection was closed: An unexpected error occurred" and there's nothing showing in Fiddler for this line, whereas the "DownloadString" for google.com works and I see the console output for that.
Following other suggestions for the error, I have tried combinations of setting UseDefaultCredentials, Encoding options, adding appropriate headers to the request, none of which make any difference.
client.UseDefaultCredentials = true;
client.Encoding = Encoding.UTF8;
When I navigate to the myServiceURL in a browser, it works and shows "OK", as expected.
Another method from the same service has been coded as follows:
// Request
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(myServiceURL + ";login");
// Set the request configuration options
req.Method = "POST";
req.ContentType = "text/xml";
req.ContentLength = bytes.Length;
req.Timeout = -1;
// Call for the request stream
using (Stream os = req.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length);
}
// ....snip
// This line fails with the same error as before
WebResponse resp = req.GetResponse()
This is all being run on a Windows 7 (64-bit) PC using .NET Framework 4.0; the service at myServiceURL is a 3rd-party service for which I have no control over.
Have finally got to the bottom of this and whilst the answer may not apply to everyone with the same problem; I would suggest that the clue was in the fact that we could pull information from some HTTPS site, but not all and tracing events through a combination of Fiddler, WireShark and our Firewall.
Opening the sites in Google Chrome and clicking the padlock for 'https' in the URL address for the site, to view the 'Security Overview' we see that for most of the sites we tried that there are entries listed for 'Valid Certificate' and for 'Secure Resources', but this one site also had an entry for 'Secure TLS Connection' and WireShark confirmed that the handshake (from Chrome) was using TLS v1.2
TLS v1.2 appears to only be supported with .NET Framework 4.5 (or above), which therefore needs Visual Studio 2012 (or above)
We are currently running .NET Framework 4.0 with Visual Studio 2010
Downloaded Visual Studio 2015 Community Edition to test the "same" code within a project using .NET Framework 4.5.2 and it worked straight away.
//assuming this is set
byte[] Data;
string url = string.Format("{0};{1}" ,myServiceURL, "login");
// Request
HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(url);
wreq.Method = "POST";
wreq.Proxy = WebProxy.GetDefaultProxy();
(wreq as HttpWebRequest).Accept = "text/xml";
if (Data != null && Data.Length > 0)
{
wreq.ContentType = "application/x-www-form-urlencoded";
System.IO.Stream request = wreq.GetRequestStream();
request.Write(Data, 0, Data.Length);
request.Close();
}
WebResponse wrsp = wreq.GetResponse();

Microsoft Live custom C# REST API error 415

I'm trying to get data from the Microsoft Live API. However, when I try to get the access_token, I instead get a 415(Unsupported Media Type) error message. I have looked pretty much everywhere, but I can't find any answer (that worked for me).
Here is my (partial) code that tries to get the token (dataToWrite is cut-up for readability, it's one line in the actual code):
WebRequest request;
request = WebRequest.Create("https://login.live.com/oauth20_token.srf");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
var dataToWrite = "code=[code]&
client_id=[client_id]&
client_secret=[client_secret]&
redirect_uri=[redirect_uri]&
grant_type=authorization_code";
var buffer = Encoding.ASCII.GetBytes(dataToWrite);
request.ContentLength = buffer.Length;
var dataStream = request.GetRequestStream();
dataStream.Write(buffer, 0, buffer.Length);
dataStream.Close();
var response = request.GetResponse();
var responseStream = response.GetResponseStream();
Where the '[]' are:
[code] is a string, given by Microsoft after user logs in (this part of the code works);
[client_id] is a string, given by Microsoft, representing my client id;
[client_secret] is a string, given by Microsoft, representing my client secret;
[redirect_uri] is the URL of the site's return location (same as the URL used in the code for the user consent(see [code]))
According to the manual of Microsoft Live API(http://msdn.microsoft.com/en-us/library/live/hh243647.aspx) this should work. However, the documentation isn't very detailed.
Does anyone know why I keep getting the error?
Thanks!
Never mind, I'm an idiot...
It does work after all. I did another request after this one. And that one failed because I did not include the parameters there.

obtain rss response

how would I be able to do something like http://myanimelist.net/modules.php?go=api#verifycred
aka "curl -u user:password http://myanimelist.net/api/account/verify_credentials.xml"
I wish to option the id
my code so far is
string url = "http://myanimelist.net/api/account/verify_credentials.xml";
WebRequest request = WebRequest.Create(url);
request.ContentType = "xml";
request.Method = "GET";
request.Credentials = new NetworkCredential(username, password);
byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes("xml/user/id"); // i think this line?
Stream reqstr = request.GetRequestStream();
reqstr.Write(buffer, 0, buffer.Length);
reqstr.Close();
but I get a error on "reqstr.Write(buffer, 0, buffer.Length)"
Cannot send a content-body with this verb-type, I have tried googling but with no prevail, I am using c#
Your snippet is trying to send a GET request with request data (you're calling GetRequestStream and then writing some data to the request stream). The HTTP protocol does not allow this - you can only send data with POST request.
However, the API that you are trying to call is actually doing something different - you do not need to send it the XML data. The XML data (with user ID and user name) is the response that you get when you successfully login.
So, instead of calling GetRequestStream and writing the XML data, you need to call GetResponse and then GetResponseStream to read the XML data!

Categories

Resources