Share via


Connect to API and wait to response

Question

Friday, November 13, 2015 11:53 PM

I have a Windows Form and want to check if it can connect to API called, as example, https://somename/api/v123/valid_app_id?app_id=12345

Here's my code:

var httpWReq = (HttpWebRequest)WebRequest.Create("https://somename/api/v123/valid_app_id?app_id=12345");
var postData = "";
var encoding = new ASCIIEncoding();
var data = encoding.GetBytes(postData);
httpWReq.Method = "POST";
httpWReq.ContentType = "application/json; charset=utf-8";
httpWReq.ContentLength = data.Length;

using (var strem = httpWReq.GetRequestStream())
{
strem.Write(data, 0, data.Length);
}

var response = (HttpWebResponse)httpWReq.GetResponse();
var result = new StreamReader(response.GetResponseStream()).ReadToEnd();

It shows an exception: "the remote server returned an error (400) not found".

This is in line

var response = (HttpWebResponse)httpWReq.GetResponse();

I send a request using 'Postman' request builder and got "message": "1", which is correct.

Any help to resolve this?

Thanks in advance!

All replies (7)

Tuesday, November 17, 2015 6:24 AM âś…Answered

Can you try the below simple GET request, since you are reading info:

HttpWebRequest http = (HttpWebRequest)WebRequest.Create("http://example.com");
WebResponse response = http.GetResponse();
MemoryStream stream = response.GetResponseStream();
StreamReader sr = new StreamReader(stream);
string content = sr.ReadToEnd();

Postman should allow you to intercept your request, you have to check that.

In case its an asp.net web api  there is a dedicated forum for that under, which you can refer to also:

Web api

Fouad Roumieh


Sunday, November 15, 2015 9:05 PM

hmm, never used Postman. Does it run locally?

Then you can use Fiddler to intercept the calls to that API and also from your code and compare them.


Sunday, November 15, 2015 10:08 PM

var httpWReq = (HttpWebRequest)WebRequest.Create("https://somename/api/v123/valid_app_id?app_id=12345");

Well, the URL with the definition of the parm is not correct, which will lead to a 400 meaning the site cannot be found, because of the URL.

I suggest that you go to a browser, enter the URL in the address line with the parm and see if you can make contact. **


Monday, November 16, 2015 7:38 AM

Hi Ruben ,

>>It shows an exception: "the remote server returned an error (400) not found".

In order to check if those setings are ok, do following:
Open internet explorer (or another) browser and go to
https://somename/api/v123/valid_app_id?app_id=12345
You should get the Portal homepage with login screen.
If you don't - then you have a problem with your local portal ( may be it is not running ).
If you do - Login with username: administrator and password: XXX
If you don't succeed to login, then you have a problem with your user( may be it does not exist ).
Regards,
Kristin

We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.


Tuesday, November 17, 2015 3:43 AM

Hi,

I already typed the URL in the browser and I can make contact. I could returns 

{"message":"0"} or 
{"message":"1"}

Tuesday, November 17, 2015 4:33 AM

Hi,

I already typed the URL in the browser and I can make contact. I could returns 

{"message":"0"} or 
{"message":"1"}

Well then, you need to install Fiddler, have it up and running, run your program and look at the raw URL when the 400 is thrown and shown in Fiddler to see what the URL with parm is sent by your program.

http://www.infragistics.com/community/blogs/anton_staykov/archive/2010/08/24/how-to-debug-your-application-http-protocol-using-fiddler.aspx


Tuesday, November 17, 2015 4:33 PM

Well, It worked. Returns the value I want. Thanks.

I had to change this:

MemoryStrem stream = response.GetResponseStream();

to 

Strem stream = response.GetResponseStream();