Я пытаюсь сделать запрос httppost в службу WCF Restful с Android. Я создаю XML в Android и передаю его службе через httppost. Однако я продолжаю получать указанное выше исключение.
Я создал службу WCF Rest с методом POST, который содержит функцию для чтения содержимого полученного xml.
Кроме того, я включил домен в WWW под брандмауэром. Я могу просматривать сервис, если использую URL-адрес http://192.168.0.12/RestFul/service1.svc, но когда я пытаюсь вызвать веб-метод вместе со службой из Android, например http://192.168.0.12/RestFul/service1.svc/Push Я получаю это исключение
Вот мой код для запроса Httppost:
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
dbfac.setNamespaceAware(true);
DocumentBuilder docBuilder = null;
try {
docBuilder = dbfac.newDocumentBuilder();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DOMImplementation domImpl = docBuilder.getDOMImplementation();
doc = domImpl.createDocument(null,"SensorData", null);
Log.v("MA", "File created:"+doc);
doc.setXmlVersion("1.0");
doc.setXmlStandalone(true);
Element trackElement = doc.getDocumentElement();
Element CompanyId = doc.createElement("CompanyId");
CompanyId.appendChild(doc.createTextNode("1"));
trackElement.appendChild(CompanyId);
Log.v("MA", "Element added"+CompanyId);
Element CreatedBy = doc.createElement("CreatedBy");
CreatedBy.appendChild(doc.createTextNode("6"));
trackElement.appendChild(CreatedBy);
Log.v("MA", "Element added"+CreatedBy);
Element DepartmentId = doc.createElement("DepartmentId");
DepartmentId.appendChild(doc.createTextNode("4"));
trackElement.appendChild(DepartmentId);
Log.v("MA", "Element added"+DepartmentId);
Element IsBillable = doc.createElement("IsBillable");
IsBillable.appendChild(doc.createTextNode("1"));
trackElement.appendChild(IsBillable);
Log.v("MA", "Element added"+IsBillable);
Element ProjectId = doc.createElement("ProjectId");
ProjectId.appendChild(doc.createTextNode("1"));
trackElement.appendChild(ProjectId);
Log.v("MA", "Element added"+ProjectId);
Element StartTime = doc.createElement("StartTime");
StartTime.appendChild(doc.createTextNode("2015-09-27 10:44:45"));
trackElement.appendChild(StartTime);
Log.v("MA", "Element added"+StartTime);
Element StopTime = doc.createElement("StopTime");
StopTime.appendChild(doc.createTextNode("2015-09-27 11:44:45"));
trackElement.appendChild(StopTime);
Log.v("MA", "Element added"+StopTime);
Element TaskId = doc.createElement("TaskId");
TaskId.appendChild(doc.createTextNode("3"));
trackElement.appendChild(TaskId);
Log.v("MA", "Element added"+TaskId);
Element TotalTime = doc.createElement("TotalTime");
TotalTime.appendChild(doc.createTextNode("1"));
trackElement.appendChild(TotalTime);
Log.v("MA", "Element added"+TotalTime);
Element TrackDesc = doc.createElement("TrackDesc");
TrackDesc.appendChild(doc.createTextNode("dello testing"));
trackElement.appendChild(TrackDesc);
Log.v("MA", "Element added"+TrackDesc);
Element TrackId = doc.createElement("TrackId");
TrackId.appendChild(doc.createTextNode("0"));
trackElement.appendChild(TrackId);
Log.v("MA", "Element added"+TrackId);
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = null;
try {
trans = transfac.newTransformer();
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
//create string from xml tree
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
try {
trans.transform(source, result);
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
xmlString = sw.toString();
// Create a new HttpClient and Post Header
DefaultHttpClient httpClient = new DefaultHttpClient();
httppost = new HttpPost("http://192.168.0.12/RestFul/service1.svc/Push/");
Log.v("MA", "xmlString is:"+xmlString);
// Make sure the server knows what kind of a response we will accept
httppost.addHeader("Accept", "text/xml");
// Also be sure to tell the server what kind of content we are sending
httppost.addHeader("Content-Type", "application/xml");
try
{
StringEntity entity = new StringEntity(xmlString, "UTF-8");
entity.setContentType("text/xml");
httppost.setEntity(entity);
Log.v("MA", "Post executed");
// execute is a blocking call, it's best to call this code in a thread separate from the ui's
HttpResponse response = httpClient.execute(httppost);
responseCode = response.getStatusLine().getStatusCode();
responseContent = EntityUtils.toString(response.getEntity());
Log.v("MA", "HttpResponse:"+response);
//Toast.makeText(getApplicationContext(),"Response:"+response, Toast.LENGTH_LONG);
BasicResponseHandler responseHandler = new BasicResponseHandler();
String strResponse = null;
if (response != null) {
try {
Log.v("MA", "Response is not null");
strResponse = responseHandler.handleResponse(response);
// Toast.makeText(getApplicationContext(), "Response:",Toast.LENGTH_SHORT).show();
} catch (HttpResponseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Log.e("WCFTEST", "WCFTEST ********** Response" + strResponse);
[![enter image description here][1]][1]
}
catch (Exception ex)
{
ex.printStackTrace();
}
Вот мой метод веб-сервиса:
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "Push",
RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare)]
XElement DoWork(Stream xml);
public XElement DoWork(Stream xml)
{
StreamReader reader = null;
XDocument xDocRequest = null;
string strXmlRequest = string.Empty;
reader = new StreamReader(xml);
strXmlRequest = reader.ReadToEnd();
xDocRequest = XDocument.Parse(strXmlRequest);
string response = "<Result>OK</Result>";
return XElement.Parse(response);
}
Вот мой логарифм:
Любые предложения, чтобы получить эту работу, будут действительно оценены.