2011-12-28 53 views
2

我試着從Android使用WCF,我的代碼於Android,壞請求錯誤

private final static String SERVICE_URI = "http://188.59.2.211:8081/Survey.svc"; 

String email = "[email protected]"; 
String password = "123"; 

public void onResume() { 
     super.onResume(); 

     Login(email,password); 
    } 

private void Login(String email, String password) 
    { 
     try { 

     URL url = new URL(SERVICE_URI + "/Login/email="+email+"&password="+password); 
      HttpGet request = new HttpGet(url.toURI()); 
      request.setHeader("Accept", "application/json"); 
      request.setHeader("Content-type", "application/json"); 

      HttpClient httpClient = new DefaultHttpClient(); 
      HttpResponse response = (HttpResponse) httpClient.execute(request); 
      if (response != null) 
      { 
       Log.i("login", "received " + response.toString()); 
      } 
      else 
      { 
       Log.i("login", "got a null response"); 
      } 

      HttpEntity responseEntity = response.getEntity(); 

      // Read response data into buffer 
      char[] buffer = new char[(int)responseEntity.getContentLength()]; 
      InputStream stream = responseEntity.getContent(); 
      InputStreamReader reader = new InputStreamReader(stream); 
      reader.read(buffer); 
      stream.close(); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

和WCF服務端消費WCF,

[ServiceContract(Namespace = "http://saas.com")] 
public interface ISurvey 
{ 
    [OperationContract] 
    [WebGet(
     UriTemplate = "/Login/email={email}&password={password}", 
     BodyStyle = WebMessageBodyStyle.WrappedRequest, 
     ResponseFormat = WebMessageFormat.Json, 
     RequestFormat = WebMessageFormat.Json)] 
    LoginResult Login(string email, string password); 
} 

和我的配置,

<system.serviceModel> 
<behaviors> 
    <endpointBehaviors> 
    <behavior name="httpBehavior"> 
     <webHttp /> 
    </behavior> 
    </endpointBehaviors> 
    <serviceBehaviors> 
    <behavior> 
     <serviceMetadata httpGetEnabled="true"/> 
     <serviceDebug includeExceptionDetailInFaults="false"/> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
<services> 
    <service name="SAASService.Survey"> 
    <endpoint address="" 
       behaviorConfiguration="httpBehavior" 
       binding="webHttpBinding" 
       contract="SAASService.ISurvey" /> 
    <endpoint address="mex" 
       binding="mexHttpBinding" 
       contract="IMetadataExchange" /> 
    </service> 
</services> 

問題的是,進出口使用下面的URL,

「http://188.59.2.211:8081/Survey.svc/Login/[email protected] &密碼= 123」

,它始終是給出「錯誤的請求」錯誤,statusCode = 400

出了什麼問題?

回答

2

調用類似於Android的Web服務將無法正常工作。另外,SOAP是xml,而不是json,所以你的內容類型是錯誤的。

要在Android中使用Web服務,最好使用ksoap-android庫。 你可以檢查this tutorial的示例代碼。

這是我在最近的一個項目中使用的示例代碼。

private static final String NAMESPACE = "http://webService.protocols.users.mydomain.com/"; 
private static final String URL = "http://1.1.1.1/domain/ws/userServices?wsdl"; 
private static final String SOAP_ACTION = ""; 

public boolean authenticate(String username, String password) { 
    String METHOD_NAME = "authenticate"; 

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
    request.addProperty("arg0", username); 
    request.addProperty("arg1", password); 

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
    envelope.setOutputSoapObject(request); 

    HttpTransportSE httpTransport = new HttpTransportSE(URL); 
    httpTransport.debug = true; 
    try { 
     httpTransport.call(SOAP_ACTION, envelope); 
    } catch (Exception e) { 
     return false; 
    } 

    SoapObject result = null; 
    try { 
     result = (SoapObject) envelope.getResponse(); 
    } catch (SoapFault soapFault) { 
     return false; 
    } 

    // anything below this line relates to the service return type, may not apply to you 
    boolean success = Boolean.valueOf(result.getProperty("success").toString()); 
    if (success) 
     sessionId.set(result.getProperty("sessionId").toString()); 
    else 
     error_message.set(result.getProperty("errorMessage").toString()); 

    return success; 
}