gpt4 book ai didi

android - 从 Android 使用 Wcf,错误请求错误

转载 作者:行者123 更新时间:2023-11-29 00:42:13 25 4
gpt4 key购买 nike

我正在尝试从 Android 中使用 Wcf,我的代码

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

String email = "xx@gmail.com";
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 服务端,

[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=xx@gmail.com&password=123”

它总是给出“Bad Request”错误,statusCode = 400

怎么了?

最佳答案

从 Android 调用这样的网络服务是行不通的。另外,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;
}

关于android - 从 Android 使用 Wcf,错误请求错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8650793/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com