java - 400 Bad Request HTTP Response using a WCF POST via Android -
facing issues calling wcf service android. web.config:
<?xml version="1.0"?> <configuration> <system.web> <compilation debug="true" targetframework="4.0" /> <httpruntime targetframework="4.0"/> </system.web> <system.servicemodel> <behaviors> <endpointbehaviors> <behavior name="httpbehavior"> <webhttp helpenabled="true" automaticformatselectionenabled="false" /> </behavior> </endpointbehaviors> <servicebehaviors> <behavior> <servicemetadata httpgetenabled="true" httpsgetenabled="true"/> <servicedebug includeexceptiondetailinfaults="true"/> </behavior> <behavior name="webbehavior"> </behavior> </servicebehaviors> </behaviors> <servicehostingenvironment multiplesitebindingsenabled="true"/> <services> <service name="gatep.service1"> <endpoint name="webhttpbinding" address="" binding="webhttpbinding" contract="gatep.iservice1" behaviorconfiguration="httpbehavior" /> </service> </services> <bindings> <webhttpbinding> <binding maxreceivedmessagesize="10000000" receivetimeout="01:00:00"> <readerquotas maxstringcontentlength="10000000" /> </binding> </webhttpbinding> </bindings> </system.servicemodel> <system.webserver> <modules runallmanagedmodulesforallrequests="true"/> <defaultdocument> <files> <add value="service1.svc" /> </files> </defaultdocument> <directorybrowse enabled="true"/> </system.webserver> </configuration>
iservice1.cs
[operationcontract] [webinvoke(method = "post", uritemplate = "userlogin", bodystyle = webmessagebodystyle.wrapped, responseformat = webmessageformat.json, requestformat = webmessageformat.json)] gpusers userlogin(gpusers request);
service1.svc
using system; using system.collections.generic; using system.configuration; using system.data.sqlclient; using system.linq; using system.runtime.serialization; using system.servicemodel; using system.servicemodel.web; using system.text; namespace gatep { public class service1 : iservice1 { #region gpuser login public gpusers userlogin(gpusers request) { return request; } #endregion } }
here gpuser class
using system; using system.collections.generic; using system.linq; using system.runtime.serialization; using system.web; namespace gatep { [datacontract] public class gpusers { [datamember] public int userid { get; set; } [datamember] public string username { get; set; } [datamember] public string password { get; set; } [datamember] public string department { get; set; } [datamember] public bool useractive { get; set; } } }
android code (java file)
import android.util.log; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.httpclient; import org.apache.http.client.methods.httppost; import org.apache.http.entity.stringentity; import org.apache.http.impl.client.defaulthttpclient; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.unsupportedencodingexception; /** * created arslan-pc on 4/7/2017. */ public class callwebservice { //calling web service using in json format public jsonobject jsonobjectservice(string url, jsonobject data) { jsonobject jsonresponse=null; httppost httppost = new httppost(url); httppost.setheader("content-type", "application/json"); httppost.setheader("accept", "application/json"); httpclient httpclient = new defaulthttpclient(); // building json object. /*jsonobject data = new jsonobject();*/ string result = null; stringentity entity ; try { jsonobject request=new jsonobject(); request.put("request", data); entity = new stringentity(request.tostring()); httppost.setentity(entity); // making call. httpresponse response = httpclient.execute(httppost); int status=response.getstatusline().getstatuscode(); if(status==200) { httpentity responseentity = response.getentity(); if(responseentity!=null) { inputstream stream = responseentity.getcontent(); bufferedreader buffer = new bufferedreader(new inputstreamreader(stream)); jsonresponse = new jsonobject(buffer.readline()); } } } catch (jsonexception e1) { // todo auto-generated catch block e1.printstacktrace(); } catch(unsupportedencodingexception e2) { } catch (clientprotocolexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } return jsonresponse; } }
what tried far: publish wcf service on iis using file system of visual studio. after publishing when try browsing address 192.168.5.103:12345 on mobile , desktop, looks fine when access using wcf throws status 400 bad request error. don't know main problem. attaching screenshots please have look. use line in gradle compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
wiki
Comments
Post a Comment