我成功从 WCF Web 服务获取 httpResponse。但是,它返回 html 背景设计语法。此外,该站点受身份验证限制。我确定我已经使用特定帐户进行了 https 身份验证。
该网站看起来像:https://abc/k2services/syncrest.svc/Task/items
http连接代码
public String XMLUrlHttpRequest(String Url, final String userName, final String password) {
...
try {
DefaultHttpClient httpClient =GetNewHttpClient();
//Http Request
HttpPost httpPost = new HttpPost(Url);
httpPost.setHeader("Host",host);
httpPost.setHeader("Accept","application/xml");
httpPost.setHeader("Content-Type","application/xml");
//Http Response
HttpResponse httpResponse = httpClient.execute(httpPost);
System.out.println(httpResponse.toString());
if (httpResponse.getStatusLine().getStatusCode() != 404) {
HttpEntity httpEntity = httpResponse.getEntity();
xml_string = getASCIIContentFromEntity(httpEntity);
if(xml_string.isEmpty()){
Log.w("XML", "isEmpty");
}else{
System.out.println(xml_string);
Log.w("XML", "OK");
Log.w("XML", xml_string);
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return xml_string;
}
解析和读取 XML 代码
try {
response = new ParseXML().XMLUrlHttpRequest(Url,userName,password);
}catch(Exception e){
Log.w("Cannot ", "Start");
}
String parsedData = "";
try {
Log.w("AndroidParseXMLActivity", "Start");
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
Datahandler XMLHandler = new Datahandler();
xr.setContentHandler(XMLHandler);
InputSource inStream = new InputSource();
inStream.setCharacterStream(new StringReader(response));
xr.parse(inStream);
Log.w("response ----->",response);
int i=0;
ArrayList<Data> DatasList = XMLHandler.getBooksList();
//for(;i<BooksList.size();i++){
Data data = DatasList.get(1);
parsedData = parsedData + "Detail----->\n"; // Can be shown in the application as getting httpResponse
parsedData = parsedData + "Process Name: " + data.getProcessName() + "\n"; //<---Error points to here, cause no XML content returned. No column sorted
parsedData = parsedData + "Process FullName: " + data.getProcessFullName() + "\n";
Log.w("i==",String.valueOf(i));
Log.w("Data",parsedData);
Log.w("populating", "Done");
}
catch (Exception e) {
Log.w("ParseXML",e );
}
return parsedData;
}
返回 httpResponse
<HTML><HEAD><STYLE type="text/css">#content{ FONT-SIZE: 0.7em; PADDING-BOTTOM: 2em; MARGIN-LEFT: 30px}BODY{MARGIN-TOP: 0px; MARGIN-LEFT: 0px; COLOR: #000000; FONT-FAMILY: Verdana; BACKGROUND-COLOR: white}P{MARGIN-TOP: 0px; MARGIN-BOTTOM: 12px; COLOR: #000000; FONT-FAMILY: Verdana}PRE{BORDER-RIGHT: #f0f0e0 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #f0f0e0 1px solid; MARGIN-TOP: -5px; PADDING-LEFT: 5px; FONT-SIZE: 1.2em; PADDING-BOTTOM: 5px; BORDER-LEFT: #f0f0e0 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: #f0f0e0 1px solid; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e5e5cc}.heading1{MARGIN-TOP: 0px; PADDING-LEFT: 15px; FONT-WEIGHT: normal; FONT-SIZE: 26px; MARGIN-BOTTOM: 0px; PADDING-BOTTOM: 3px; MARGIN-LEFT: -30px; WIDTH: 100%; COLOR: #ffffff; PADDING-TOP: 10px; FONT-FAMILY: Tahoma; BACKGROUND-COLOR: #003366}.intro{MARGIN-LEFT: -15px}</STYLE>
<TITLE>Service</TITLE></HEAD><BODY>
<DIV id="content">
<P class="heading1">Service</P>
<BR/>
<P class="intro">Method not allowed.</P>
</DIV>
</BODY></HTML>
谢谢