I’ve got pdf with a description of API. I must login into their webservice. Webservice is based on REST protocol. To login into this webservice I have to call url like this:
http://api.webepartners.pl/wydawca/Authorize?login=test&password=pass
I have account and password. When I replace test and pass with my login and psw and past url into webbrowser it looks like is ok. No errors occur.
But I must do it programatically in C#.
In google I’ve found:
http://developer.yahoo.com/dotnet/howto-rest_cs.html
I try this code:
Uri address = new Uri(@"http://api.webepartners.pl/wydawca/Authorize");
// Create the web request
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
// Set type to POST
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
// Create the data we want to send
//string appId = "YahooDemo";
//string context = "Italian sculptors and painters of the renaissance"
// + "favored the Virgin Mary for inspiration";
//string query = "madonna";
string userName = "mylogin";
string passsword = "mypassword";
StringBuilder data = new StringBuilder();
//data.Append("appid=" + HttpUtility.UrlEncode(appId));
//data.Append("&context=" + HttpUtility.UrlEncode(context));
//data.Append("&query=" + HttpUtility.UrlEncode(query));
data.Append("login=" + HttpUtility.UrlEncode(userName));
data.Append("&password=" + HttpUtility.UrlEncode(passsword));
// Create a byte array of the data we want to send
byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());
// Set the content length in the request headers
request.ContentLength = byteData.Length;
// Write data
using (Stream postStream = request.GetRequestStream())
{
postStream.Write(byteData, 0, byteData.Length);
}
// Get response
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) // error
{
// Get the response stream
StreamReader reader = new StreamReader(response.GetResponseStream());
}
And I got error in the line using (HttpWebResponse response = request.GetResponse() as ..
An exception of type 'System.Net.WebException' occurred in System.dll but was not handled in user code
Additional information: The remote server returned an error: (405) Method Not Allowed.
Can anyone help me ?
Thanks
You are sending a POST request with username and password as part of the request body. But it seems that the web service expects a GET service where everything is in the URL of the request.
…..
…..
That’s it?