使用kso​​ap2-android订阅sharepoint webservice时的身份验证出错

时间:2011-05-11 18:48:34

标签: android authentication sharepoint-2010 android-ksoap2 ksoap

我正在编写一个Android应用程序,它将使用sharepoint 2010中lists.amx服务的getlist()方法。我正在使用kso​​ap2-android来处理我的soap消息。当我尝试进行身份验证时,我得到一个xmlpullparser异常,预计START_TAG ... 为什么以下代码不会对sharepoint服务器进行身份验证?

这是我的代码:

public class SharepointList extends Activity {
private static final String SOAP_ACTION = "http://schemas.microsoft.com/sharepoint/soap/GetList";
private static final String METHOD_NAME = "GetList";
private static final String NAMESPACE = "http://schemas.microsoft.com/sharepoint/soap/" ;
private static final String URL = "http://<ip of sharepoint server>/_vti_bin/lists.asmx";

private TextView result;
private Button btnSubmit;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    result = (TextView)findViewById(R.id.textView1);
    btnSubmit = (Button)findViewById(R.id.button1);
    btnSubmit.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            if(v.getId() == R.id.button1)
            {
                String list = getMobileTestList();
                result.setText(list);
            }

        }

    });


}
private String getMobileTestList()
{
    PropertyInfo pi = new PropertyInfo();
    pi.setName("listName");
    pi.setValue("Mobile Test List");

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    request.addProperty(pi);

    String authentication = android.util.Base64.encodeToString("username:password".getBytes(), android.util.Base64.DEFAULT);
    List<HeaderProperty> headers = new ArrayList<HeaderProperty>();
    headers.add(new HeaderProperty("Authorization","Basic " +authentication));
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);


    HttpTransportSE transport = new HttpTransportSE(URL);
    try
    {
        transport.debug = true;
        transport.call(SOAP_ACTION, envelope, headers);
        //transport.call(SOAP_ACTION, envelope);
        Object result = envelope.getResponse();
        return result.toString();

    }
    catch(Exception e)
    {
        return e.toString();
    }
}
}

这是transport.requestdump(在'&lt;'去除之前):

  • v:Envelope xmlns:i =“http://www.w3.org/2001/XMLSchema-instance”xmlns:d =“http://www.w3.org/2001/XMLSchema”xmlns:c = “http://schemas.xmlsoap.org/soap/encoding/”xmlns:v =“http://schemas.xmlsoap.org/soap/envelope/”&gt;
    • v:标题/&gt;
    • ν:身体与GT;
      • GetList xmlns =“http://schemas.microsoft.com/sharepoint/soap/”id =“o0”c:root =“1”&gt;
        • listName i:type =“d:string”&gt;移动测试列表
    • /的GetList&GT;
    • / V:车身与GT;
  • / V:信封&GT;

这是transport.responsedump(在'&lt;'去除之前):

  • !DOCTYPE HTML PUBLIC“ - // W3C // DTD HTML 4.01 // EN”“http://www.w3.org/TR/html4/strict.dtd”&gt;
    • HTML&GT;
    • HEAD&GT;
      • TITLE&gt;错误请求
      • META HTTP-EQUIV =“Content-Type”Content =“text / html; charset = us-ascii”&gt;
    • / HEAD&GT;
    • BODY&GT;
      • h2&gt;错误请求 - 无效主机名
      • hr的GT; p&gt; HTTP错误400.请求主机名无效。

    • / BODY&GT;
  • / HTML&GT;

1 个答案:

答案 0 :(得分:3)

也许尝试以下方法:

String authentication = android.util.Base64.encodeToString("username:password".getBytes(), android.util.Base64.NO_WRAP);

默认情况下,Android Base64 util会在编码字符串的末尾添加换行符。这会使HTTP标头无效并导致“错误请求”。

Base64.NO_WRAP标志可以防止这种情况并使标题保持清晰。