In android I am posting xml data to server but getting 403 error -
i need verify user making post request. posting xml data using using rest protocol getting 403 error. please me find out doing wrong, below code post.
public void makepostconnection(string fileurl, hashmap<string, string> requestdata) { url url; httpurlconnection urlconnection; try { url = new url(fileurl); urlconnection = (httpurlconnection) url.openconnection(); urlconnection.setrequestmethod("post"); urlconnection.setrequestproperty("content-type", "application/x-www-form-urlencoded"); httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(fileurl); httppost.addheader("accept", "text/xml"); list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>( requestdata.size()); system.out.println("size= " + requestdata.size()); (entry<string, string> entry : requestdata.entryset()) { namevaluepairs.add(new basicnamevaluepair(entry.getkey(), entry .getvalue())); system.out.println("key= " + entry.getkey() + " value= " + entry.getvalue()); } system.out.println("request ba= " + namevaluepairs.tostring()); stringentity requestbody = new stringentity( namevaluepairs.tostring()); // requestbody.setcontenttype("text/xml"); // httppost.setentity(new urlencodedformentity(namevaluepairs)); httppost.setentity(requestbody); // httppost.set // execute http post request httpresponse response = httpclient.execute(httppost); int code = response.getstatusline().getstatuscode(); system.out.println("code= " + code); } catch (malformedurlexception e1) { // todo auto-generated catch block e1.printstacktrace(); } catch (ioexception e1) { // todo auto-generated catch block e1.printstacktrace(); } }
hi solve problem changing line set httppost.addheader("content-type","application/x-www-form-urlencoded"); , make urlencoded request doing urlencodedformentity ent = new urlencodedformentity(namevaluepairs,http.utf_8); below code make successful request.
try { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(fileurl); httppost.addheader("content-type", "application/x-www-form-urlencoded"); list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>( requestdata.size()); system.out.println("size= " + requestdata.size()); (entry<string, string> entry : requestdata.entryset()) { namevaluepairs.add(new basicnamevaluepair(entry.getkey(), entry .getvalue())); system.out.println("key= " + entry.getkey() + " value= " + entry.getvalue()); } urlencodedformentity ent = new urlencodedformentity(namevaluepairs, http.utf_8); httppost.setentity(ent); httpresponse response = httpclient.execute(httppost); int code = response.getstatusline().getstatuscode(); system.out.println("code= " + code); } catch (malformedurlexception e1) { // todo auto-generated catch block e1.printstacktrace(); } catch (ioexception e1) { // todo auto-generated catch block e1.printstacktrace(); } }
Comments
Post a Comment