c# - POST to REST API from winform returns empty -
i have project uses hockeystreams.com api. having trouble using api login winform. gives me blank response, whether give bad username/password combination or correct one. code follows:
public static bool login(string username, string password) { string url = string.format("{0}&username={1}&password={2}", global.login_api, username, password); httpwebrequest webrequest = (httpwebrequest)httpwebrequest.create(url); webrequest.contenttype = "application/json; charset=utf-8"; webrequest.accept = "application/json, text/javascript, */*"; webrequest.method = "post"; try { webresponse response = webrequest.getresponse(); stream stream = response.getresponsestream(); string json = ""; using (streamreader reader = new streamreader(stream)) { while (!reader.endofstream) { json += reader.readline(); } } loginjson deserializedlogindata = jsonconvert.deserializeobject<loginjson>(json); if (deserializedlogindata.status == "success") { global.token = deserializedlogindata.token; global.username = deserializedlogindata.username; global.ispremium = deserializedlogindata.membership == "regular" ? false : true; global.favoriteteam = deserializedlogindata.favteam; return true; } else { return false; } }
i have tried changing post put, throws 406 error (bad request). green json don't know go here. appreciated!
edit: found on hockeystreams website similar issue using apache httpclient.
buckinpgh's resolved issue. else out there wondering, solution (if using apache httpclient) create httppost request url: "https://api.hockeystreams.com/login?" add 2 headers: 1) stringentity value: "username=<username>&password=<password>&key=<apikey>" 2) "content-type", "application/x-www-form-urlencoded" after can call httpclient.execute request. of course you'll have parse json result, @ least have result.
i changed code try , follow best httpwebrequest:
public static bool login(string username, string password) { string url = global.login_path; httpwebrequest webrequest = (httpwebrequest)httpwebrequest.create(url); webrequest.contenttype = "application/x-www-form-urlencoded"; webrequest.accept = "application/json, text/javascript, */*"; webrequest.method = "post"; webrequest.headers.add("stringentity", string.format("username={0}&password={1}&key={2}", username, password, global.apikey); try { webresponse response = webrequest.getresponse(); stream stream = response.getresponsestream(); string json = "";
but still comes blank response. there different way around i'm not seeing?
edit 2:
ended code fixing it. used restsharp this.
public static bool login(string username, string password) { string url = global.login_path; try { var client = new restclient("https://api.hockeystreams.com/"); var request = new restrequest("login?", method.post); request.addheader("content-type", "application/x-www-forms-urlencoded"); request.addparameter("username", username); request.addparameter("password", password); request.addparameter("key", global.apikey); irestresponse response = client.execute(request); var json = response.content; loginjson deserializedlogindata = jsonconvert.deserializeobject<loginjson>(json); try { if (deserializedlogindata.status == "success") { global.token = deserializedlogindata.token; global.username = deserializedlogindata.username; global.ispremium = deserializedlogindata.membership == "regular" ? false : true; global.favoriteteam = deserializedlogindata.favteam; return true; } else { global.error = deserializedlogindata.msg; return false; } }
try putting key in url (outlined documentation):
https://api.hockeystreams.com/login?username=<username>&password=<password>&key=<api_key>
Comments
Post a Comment