Skip to main content

Using powershell to get the current token of a user I do not have problem:

$uri = "http://myserver/fmetoken/view"
$postParams = @{user='Myuser';password='PS78U-w&'}
$foo = Invoke-WebRequest -Uri $uri -Method Post -Body $postParams

$foo.Content

i receive the token.

if I use postman to verify it, there is no issue.

but if I want to implement the same action in c# i always have a bad request:

 

var url = $"{fmeServerUrl}/fmetoken/view";
var body = $"user={settingsUserFme}&password={settingsPasswordFme}";

var bodyencoded = System.Net.WebUtility.UrlEncode(body);
var byteArray = Encoding.UTF8.GetBytes(bodyencoded);
var responseCode = 0;
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.Credentials = CredentialCache.DefaultCredentials;
request.ContentType = "application/x-www-form-urlencoded";

request.ContentLength = byteArray.Length;
var dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();

WebResponse response = request.GetResponse();
using (var reader = new StreamReader(response.GetResponseStream()))
{
responseresult = reader.ReadToEnd();
reader.Close();
}

 

I know this is not an FME specific problem, but if you can give me a hint, it would be appreciated

I suspect the problem is that you're URL-encoding the entire data block, where as you should only encode the values, e.g.

var body = "user=" + Uri.EscapeDataString(settingsUserFme) + 
            "&password=" + Uri.EscapeDataString(settingsPasswordFme)

Reply