我使用WCF Web服務在Android中......我 這裏是我的Java代碼來驗證使用Windows身份驗證的用戶Windows身份驗證在android系統
public void test3()
{
try
{
DefaultHttpClient httpClient = new DefaultHttpClient();
URI uri = new URI("http://10.0.2.2/MicrosoftDynamicsAXAif60/testgroup/xppservice.svc");
// Send GET request to <service>/GetText
HttpPost httpget = new HttpPost(uri);
StringEntity se = null;
// declare it as XML
se = new StringEntity(this.returnXML(), HTTP.UTF_8);
se.setContentType("text/xml");
httpget.setHeader("Content-Type", "application/xml;charset=UTF-8");
httpget.setEntity(se);
//NTCredentials nt = new NTCredentials("username", "pass", "", "domain");
httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, nt);
HttpResponse response = httpClient.execute(httpget);
HttpEntity responseEntity = response.getEntity();
// Read response data into buffer
long intCount = responseEntity.getContentLength();
char[] buffer = new char[(int)intCount];
InputStream stream = responseEntity.getContent();
InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
try
{
reader.read(buffer);
String str = new String(buffer);
Toast.makeText(this, str, 10);
}
catch (IOException e)
{e.printStackTrace();}
stream.close();
}
catch (Exception e) {e.printStackTrace();}
}
現在越來越HTTP錯誤401.2 - 未經授權
這裏的xml配置
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="wsHttpBindingWithWindowsAuth">
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="Windows" >
<extendedProtectionPolicy protectionScenario="TransportSelected" policyEnforcement="Always" />
</transport>
<message clientCredentialType="Windows" />
</security>
</binding>
</wsHttpBinding>
<basicHttpBinding>
<binding name="basicHttpBindingWithWindowsAuth">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" />
</security>
</binding>
</basicHttpBinding>
<netTcpBinding>
<binding name="DefaultServiceGroupBinding" closeTimeout="00:05:00" openTimeout="00:05:00" receiveTimeout="00:10:00" sendTimeout="00:05:00" transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions" hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="524288" maxBufferSize="81920000" maxConnections="10" maxReceivedMessageSize="81920000">
<readerQuotas maxDepth="32000000" maxStringContentLength="81920000" maxArrayLength="163840000" maxBytesPerRead="4096000" maxNameTableCharCount="16384000" />
<reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
<security mode="Transport">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
<message clientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding>
</bindings>
<services>
<!--ROUTING SERVICE -->
<!--Define a routing service. The Behavior Configuration field references the name of the Routing Behavior that this service will use-->
<service name="System.ServiceModel.Routing.RoutingService" behaviorConfiguration="routingData">
<!--
Define and configure the endpoint we want the Router to listen on and the Contract we want it to use
Router provided contracts are: ISimplexDatagramRouter, ISimplexSessionRouter, IRequestReplyRouter, and IDuplexSessionRouter.
-->
<endpoint address=""
binding="basicHttpBinding"
bindingConfiguration="basicHttpBindingWithWindowsAuth"
name="reqReplyEndpoint"
contract="System.ServiceModel.Routing.IRequestReplyRouter" >
<identity>
<servicePrincipalName />
</identity>
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="routingData">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="False" />
<!--
Define the Routing Behavior. The routingTableName attribute contains the name of the Routing Table that this behavior will use.
<routing filterTableName="AosRoutingTable" />
-->
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="clientEndpointBehavior">
<clientEndpointBehavior />
</behavior>
</endpointBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add
name="clientEndpointBehavior"
type="Microsoft.Dynamics.Ax.Services.Host.ClientEndpointBehaviorExtension, Microsoft.Dynamics.IntegrationFramework.WebService.Process, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</behaviorExtensions>
</extensions>
<!--
Define the client endpoints that we want the Router to communicate with. These are the destinations that the Router will send messages to.
Note that we always define the client endpoint contract as *. The router will mirror the selected contract defied on the endpoint
on which the message was originally recieved.
-->
<client>
<endpoint name="ServiceCustom" address="net.tcp://AOS_SERVICE_HOST/DynamicsAx/Services/testgroup" binding="netTcpBinding" bindingConfiguration="DefaultServiceGroupBinding" contract="*" behaviorConfiguration="clientEndpointBehavior">
<identity><servicePrincipalName /></identity>
</endpoint>
</client>
<routing>
<!--
Define the filters that we want the router to use. In this example we define a MatchAll message filter, which will match all messages it sees.
-->
<filters>
<filter name="MatchServiceCustom" filterType="Custom" customType="Microsoft.Dynamics.Ax.Services.Host.MatchAxServiceFilter, Microsoft.Dynamics.IntegrationFramework.WebService.Process" filterData="http://tempuri.org/ServiceCustom" />
</filters>
<!-- Define the routing table that contains the matchAll filter defined in the filters section. -->
<filterTables>
<filterTable name="AosRoutingTable">
<!-- Map the filter to a client endpoint that was previously defined. Messages that match this filter will be sent to this destination. -->
<add filterName="MatchServiceCustom" endpointName="ServiceCustom" />
</filterTable>
</filterTables>
</routing>
</system.serviceModel>
</configuration>
的XML是我發送
<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
<a:Action>http://tempuri.org/ServiceCustom/CreateMyDataObject</a:Action>
<h:CallContext i:nil="true" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:h="http://schemas.microsoft.com/dynamics/2010/01/datacontracts" />
<a:MessageID>urn:uuid:c70fb5f6-1345-469d-84fd-bb24500d329a</a:MessageID>
<a:ReplyTo>
<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
</a:ReplyTo>
</s:Header>
<s:Body>
<ServiceCustomCreateMyDataObjectRequest xmlns="http://tempuri.org">
<_s>23</_s>
</ServiceCustomCreateMyDataObjectRequest>
</s:Body>
</s:Envelope>
解決!
http://mrrask.wordpress.com/2009/08/21/android-authenticating-via-ntlm/