c# - Cookies not working using WebClient on Windows Phone -
my problem when use new webclient send url webservice asks me login, , after searched issue found need save cookies not working me.
and upon request full code
namespace phoneapp8 { public partial class mainpage : phoneapplicationpage { // constructor public mainpage() { initializecomponent(); // sample code localize applicationbar //buildlocalizedapplicationbar(); } public class outerrootobject { public string d { get; set; } } public class globals { public bool multisessionsallowed { get; set; } public int commcalctype { get; set; } public int pricechangedtimer { get; set; } public int validlotslocation { get; set; } public bool custumizetrademsg { get; set; } public object firstwhitelabeledoffice { get; set; } public int dealertreepriv { get; set; } public int clientconnecttimer { get; set; } public int clienttimeouttimer { get; set; } public double defaultlots { get; set; } public string websecurityid { get; set; } public int servergmt { get; set; } } public class versioninfo { public int rel { get; set; } public int ver { get; set; } public int patch { get; set; } public int forceupdate { get; set; } public int updatetype { get; set; } public globals globals { get; set; } } public class systemlockinfo { public int minutesremaining { get; set; } public int hoursremaining { get; set; } public int daysremaining { get; set; } public int maintanance { get; set; } public int willbelocked { get; set; } } public class rootobject { public string sessionid { get; set; } public versioninfo versioninfo { get; set; } public systemlockinfo systemlockinfo { get; set; } public string firstwhitelabel { get; set; } public string wlid { get; set; } public bool checkwhitelabel { get; set; } public string password { get; set; } public string username { get; set; } public datetime lastticktime { get; set; } public int selectedaccount { get; set; } public int name { get; set; } public object servicepath { get; set; } public string gwsessionid { get; set; } public string ip { get; set; } public string sessiondatestart { get; set; } public string companyname { get; set; } public string userid { get; set; } public string democlient { get; set; } public string fname { get; set; } public string sname { get; set; } public string tname { get; set; } public string lname { get; set; } public object sms { get; set; } public string isreadonly { get; set; } public string schsms { get; set; } public string alertsms { get; set; } public object temp { get; set; } public string gmtoffset { get; set; } public string svrgmt { get; set; } public object clienttype { get; set; } public string enablenews { get; set; } public string publicslidenews { get; set; } public string privateslidenews { get; set; } public int dealertreepriv { get; set; } } private void button_click(object sender, routedeventargs e) { string baseuri = "http://ip in comment/vertexweb10/webservice.svc/login?username=%22user15650%22&password=%22898k%22"; cookieawarewebclient wc = new cookieawarewebclient(); wc.downloadstringcompleted += parseproducts_asjson; uri uri = new uri(baseuri); wc.downloadstringasync(uri); } private void parseproducts_asjson(object sender, downloadstringcompletedeventargs e) { string raw = e.result; string webresponse = e.result; messagebox.show(webresponse string); var outerroot = jsonconvert.deserializeobject<outerrootobject>(webresponse); var root = jsonconvert.deserializeobject<rootobject>(outerroot.d); messagebox.show("hello " + root.username + " " + root.userid + " " + root.sessionid); var useridval= convert.toint16 (root.userid); if (useridval == -1 || useridval == -207) messagebox.show("invalid username or password"); else if (useridval == -231) { messagebox.show("you must have @ least 1 account"); } else if (webresponse == null || webresponse == "" || useridval < 0) { messagebox.show("error while login.please try later"); } else if (useridval > 0) { string baseuri1 = "http://ip in comment/vertexweb10/webservice.svc/getallsymbols?accountid=1122336675"; cookieawarewebclient wc1 = new cookieawarewebclient(); wc1.downloadstringcompleted += parseproducts_asjson1; uri uri1 = new uri(baseuri1); wc1.downloadstringasync(uri1); messagebox.show("you have loged in"); navigationservice.navigate(new uri("/page2.xaml", urikind.relative)); } else { messagebox.show("invalid username or password"); } } private void parseproducts_asjson1(object sender, downloadstringcompletedeventargs e) { string raw1 = e.result; string webresponse1 = e.result; messagebox.show(webresponse1 string); } private void textbox_textchanged(object sender, textchangedeventargs e) { } class cookieawarewebclient : webclient { [system.security.securitysafecritical] private cookiecontainer cookie; public cookiecontainer cookie { { return cookie; } } public cookieawarewebclient() { cookie = new cookiecontainer(); } public cookieawarewebclient(cookiecontainer givencontainer) { cookie = givencontainer; } } } }
i think error in cookie container, last cookieaware use (i have tried more 10 cookie container codes)
this code, calls first url , response if response good, copies cookie sessioncookie variable. , uses in second call
declare sessioncookie in same page
public static cookiecontainer sessioncookie;
and copy in button click
public static async task<string> sendrequestgetresponse() { try { var postrequest = (httpwebrequest)webrequest.create("http://74.54.46.178/vertexweb10/webservice.svc/login?username=%22user15650%22&password=%22898k%22"); postrequest.method = "get"; if (sessioncookie == null) { postrequest.cookiecontainer = new cookiecontainer(); } else { postrequest.cookiecontainer = sessioncookie; } httpwebresponse postresponse = (httpwebresponse)await postrequest.getresponseasync(); if (postresponse != null) { var postresponsestream = postresponse.getresponsestream(); var poststreamreader = new streamreader(postresponsestream); string response = await poststreamreader.readtoendasync(); if (sessioncookie == null) { sessioncookie = postrequest.cookiecontainer; } } var postrequest1 = (httpwebrequest)webrequest.create("http://74.54.46.178/vertexweb10/webservice.svc/getallsymbols?accountid=1122336675"); postrequest1.method = "get"; if (sessioncookie == null) { postrequest1.cookiecontainer = new cookiecontainer(); } else { postrequest1.cookiecontainer = sessioncookie; } httpwebresponse postresponse1 = (httpwebresponse)await postrequest1.getresponseasync(); if (postresponse1 != null) { var postresponsestream1 = postresponse1.getresponsestream(); var poststreamreader1 = new streamreader(postresponsestream1); string response1 = await poststreamreader1.readtoendasync(); } } }
Comments
Post a Comment