Signing into drupal website remotly using C#.net and json -
i want create form in c#.net can sign me in through drupal website. service 3.x module enabled , rest server running on website correctly. problem how serialize username , password json format?
finally solution :
i've got windows form , contains textbox , masked-textbox , button (called button3 below) ,
on button click event , textbox , maskedtextbox contents placed in user object constructed using class :
class user { public string username; public string password; public string name; public string number; public string address; public string email; public user(string user, string pass, string name = "", string number = "", string address = "", string email = "") { this.username = user; this.password = pass; this.name = name; this.number = number; this.address = address; this.email = email; } }
then converted object json model using json.net library
in rest makes request incoming cookie store in cookiejar , rest requests must copy cookiecontainer request cookiecontainer , stay logged in. rest of code :
private void button3_click(object sender, eventargs e) { cookiecontainer cookiejar = new cookiecontainer(); user user = new user(textbox1.text,maskedtextbox1.text); string url = "http://"your-web-address"/"your-rest-service"/?q="your-resurce"/user/login.json"; try { var request = (httpwebrequest)webrequest.create(url); request.contenttype = "application/json"; request.method = "post"; request.cookiecontainer = cookiejar; using (var streamwriter = new streamwriter(request.getrequeststream())) { string query = jsonconvert.serializeobject(user); streamwriter.write(query); streamwriter.flush(); streamwriter.close(); } var response = (httpwebresponse)request.getresponse(); using (var streamreader = new streamreader(response.getresponsestream())) { var responsetext = streamreader.readtoend(); } //check see if you're logged in url = "http://web-address/rest-server/?q=resurce/system/connect.json"; var newrequest = (httpwebrequest)webrequest.create(url); newrequest.cookiecontainer = cookiejar; newrequest.contenttype = "application/json"; newrequest.method = "post"; var newresponse = (httpwebresponse)newrequest.getresponse(); using (var newstreamreader = new streamreader(newresponse.getresponsestream())) { var newresponsetext = newstreamreader.readtoend(); } } catch(exception ex) { messagebox.show(ex.tostring(), "can't access server"); } }
note: second request test if drupal knows me.
Comments
Post a Comment