I have this class that works correctly from a console application.
When I incorporate it into my Xamarin Forms project, it gives an error (refused connection) when creating the webrequest.
I would appreciate any help in that regard.
using (Stream stream = webRequest.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
stays dead until causing an exception in Xamarin forms.
Error image
My code:
public class WebServiceSOAP
{
var _url = "http://MyDomain/My.asmx";
var _action = "http://MyDomain/MyMethod";
string sxml = #"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/""
xmlns:adh=""http://Mydomain"">
<soapenv:Header/>
<soapenv:Body>
<adh:MyMethod>
<adh:param1>TEST1</adh:param1>
<adh:param2>test2</adh:param2>
<adh:param3>test3</adh:param3>
</adh:MyMethod>
</soapenv:Body>
</soapenv:Envelope>";
XmlDocument soapEnvelopeXml = CreateSoapEnvelope(sxml);
HttpWebRequest webRequest = CreateWebRequest(_url, _action);
InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);
using (var response = (HttpWebResponse)(webRequest.GetResponse()))
{
HttpStatusCode code = response.StatusCode;
if (code == HttpStatusCode.OK) {
using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
string resp = sr.ReadToEnd();
}
}
}
}
private static HttpWebRequest CreateWebRequest(string url, string action)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Headers.Add("SOAPAction", action);
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
return webRequest;
}
private static XmlDocument CreateSoapEnvelope(string sxml)
{
XmlDocument soapEnvelopeDocument = new XmlDocument();
soapEnvelopeDocument.LoadXml(sxml);
return soapEnvelopeDocument;
}
private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
{
using (Stream stream = webRequest.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
}
Related
I'm trying to get the response from an http api rest link, but i don't know why, the response is empty, even testing the endpoint on postman and in my browser, that returns the correct. The code i'm using for getting the response is:
private static string connect(string url, string method, string data)
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = method;
request.ContentType = "application/json";
request.Accept = "application/json";
if (data.Length > 0)
{
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
streamWriter.Write(data);
streamWriter.Flush();
streamWriter.Close();
}
}
using (WebResponse response = request.GetResponse())
{
using (Stream strReader = response.GetResponseStream())
{
if (strReader == null) return "";
using (StreamReader objReader = new StreamReader(strReader))
{
return JsonConvert.DeserializeObject<Response>(objReader.ReadToEnd()).data; //objReader.ReadToEnd();
}
}
}
}
And calling it like this:
String response = connect("http://31.214.245.211:8080/ProjectM-WS/webservice/rest/ping", "GET", "");
Any idea of where I can be wrong?
Thank you for the help!
Thought that didn't work, that's becouse i commented it, but now it does.
private static string connect(string url, string method, string data)
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = method;
request.ContentType = "application/json";
request.Accept = "application/json";
if (data.Length > 0)
{
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
streamWriter.Write(data);
streamWriter.Flush();
streamWriter.Close();
}
}
using (WebResponse response = request.GetResponse())
{
using (Stream strReader = response.GetResponseStream())
{
if (strReader == null) return "";
using (StreamReader objReader = new StreamReader(strReader))
{
return objReader.ReadToEnd();
}
}
}
}
I am unable to hit a SOAP 1.2 web service with C# code. I followed the code in this example, Web Service SOAP Call
It does not work for me. My web service URL works in SOAPUI but I can't get a response in my C# code. I get a "500 Internal error." The status reads "ProtocolError."
I am running this from my Visual Studio 2017 editor.
using System;
using System.IO;
using System.Net;
using System.Xml;
namespace testBillMatrixConsole
{
class Program
{
static void Main(string[] args)
{
string _url = #"https://service1.com/MSAPaymentService/MSAPaymentService.asmx";
var _action = #"http://schemas.service1.com/v20060103/msapaymentservice/AuthorizePayment";
try
{
XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
HttpWebRequest webRequest = CreateWebRequest(_url, _action);
InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);
// begin async call to web request.
IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);
// suspend this thread until call is complete. You might want to
// do something usefull here like update your UI.
asyncResult.AsyncWaitHandle.WaitOne();
// get the response from the completed web request.
string soapResult;
using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
{
using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
{
soapResult = rd.ReadToEnd();
}
Console.Write(soapResult);
}
}
catch (WebException ex)
{
throw;//ex.Message.ToString();
}
}
private static XmlDocument CreateSoapEnvelope()
{
XmlDocument soapEnvelopeDocument = new XmlDocument();
soapEnvelopeDocument.LoadXml(#"<soap:Envelope xmlns:soap=""http://www.w3.org/2003/05/soap-envelope"" xmlns:msap=""http://schemas.service1.com/v20060103/msapaymentservice""><soap:Body><msap:AuthorizePayment><!--Optional:--><msap:clientPaymentId>?</msap:clientPaymentId><msap:amount>?</msap:amount><msap:method>?</msap:method><!--Optional:--><msap:parameters><!--Zero or more repetitions:--><msap:PaymentParameter><!--Optional:--><msap:Name>?</msap:Name><!--Optional:--><msap:Value>?</msap:Value></msap:PaymentParameter></msap:parameters></msap:AuthorizePayment></soap:Body></soap:Envelope>");
return soapEnvelopeDocument;
}
private static HttpWebRequest CreateWebRequest(string url, string action)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Headers.Add("SOAPAction", action);
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
return webRequest;
}
private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
{
using (Stream stream = webRequest.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
}
}
}
I am trying to consume web service using SOAP request in asp.net C# but it is failing with
The remote server returned an error: (500) Internal Server Error.
Below is my web service request code:
public static HttpWebRequest CreateWebRequest()
{
string action = "http://localhost:8081/Service.asmx?op=getData";
string url = "http://localhost:8081/Service.asmx";
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Headers.Add("SOAPAction", action);
webRequest.ContentType = "application/soap+xml; charset=utf-8";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
webRequest.Timeout = 30000;
return webRequest;
}
public static XmlDocument ServiceCall()
{
HttpWebRequest request = CreateWebRequest();
XmlDocument soapEnvelopeXml = CreateSoapEnvelope("MyUser", "MyUser", "MyUser");
using (Stream stream = request.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
IAsyncResult asyncResult = request.BeginGetResponse(null, null);
asyncResult.AsyncWaitHandle.WaitOne(20 * 1000);
string soapResult;
using (WebResponse webResponse = request.EndGetResponse(asyncResult))
using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
{
soapResult = rd.ReadToEnd();
}
XmlDocument resp = new XmlDocument();
resp.LoadXml(soapResult);
return resp;
}
private static XmlDocument CreateSoapEnvelope(string userName, string password, string DCSCommand)
{
StringBuilder builder = new StringBuilder();
builder.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
builder.Append("<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">");
builder.Append("<soap12:Body>");
builder.Append("<geteFliteData xmlns=\"http://test.org\">");
builder.Append("<userName>" + userName + "</userName>");
builder.Append("<password>" + password + "</password>");
builder.Append("<DCSCommand>" + DCSCommand + "</DCSCommand>");
builder.Append("</geteFliteData>");
builder.Append("</soap12:Body>");
builder.Append("</soap12:Envelope>");
XmlDocument soapEnvelop = new XmlDocument();
soapEnvelop.LoadXml(builder.ToString());
return soapEnvelop;
}
Note: When I am consuming this web service using WSDL file it is working fine and returns expected XML
it gives me an error when I am trying to authenticate with server using NTLM in wp7 "The remote server returned an error: NotFound."
private void callWebservice(object sender, RoutedEventArgs e)
{
NetworkCredential credentials = new NetworkCredential(userName, Password, domain);
HttpWebRequest request = CreateWebRequest(url, credentials);
XDocument soapEnvelope = CreateSoapEnvelope(soapEnvelope );
InsertSoapEnvelopeIntoWebRequest(soapEnvelope, request);
}
private static HttpWebRequest CreateWebRequest(string url, NetworkCredential credentials)
{
string action = link;// my action link
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
req.Credentials = credentials;
req.Headers["SOAPAction"] = action;
req.ContentType = "text/xml;charset=\"utf-8\"";
req.Accept = "text/xml";
req.Method = "POST";
return req;
}
private static XDocument CreateSoapEnvelope(string content)
{
XDocument soapEnvelopeXml = XDocument.Parse(content);
return soapEnvelopeXml;
}
private static void InsertSoapEnvelopeIntoWebRequest(XDocument soapEnvelopeXml, HttpWebRequest webRequest)
{
webRequest.BeginGetRequestStream((IAsyncResult asynchronousResult) =>
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
Stream postStream = request.EndGetRequestStream(asynchronousResult);
soapEnvelopeXml.Save(postStream);
postStream.Close();
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}, webRequest);
}
private static void GetResponseCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
string responseString = streamRead.ReadToEnd();
//do whatever with the response
MessageBox.Show(responseString);
streamResponse.Close();
streamRead.Close();
response.Close();
}
authenticate with server using NTLM on wp7 insn't supported ,it is supported on wp8
the same code on wp8 it gives result
I am using 4shared rest api, and working on edit file information by http put. I coded like this:
public void UpdateFile(string fileID, string accessToken, int uniqueID)
{
Action<string> OnResponse = this.OnFileUpdate;
string uri = string.Format("http://api.4shared.com/v0/files/fileID.json?oauth_token= {1}&UniqueID={2}",accessToken,uniquesID);
uri=uri + "&name=mayank&description=This is for testing.";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(uri));
request.Method = "PUT";
request.ContentType = "application/x-www-form-urlencoded";
request.BeginGetResponse(delegate(IAsyncResult result)
{
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
using (var stream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream))
{
string response1 = reader.ReadToEnd();
onResponseGot(response1);
}
}
}, null);
}
private void OnFileUpdate(string result)
{
if (!string.IsNullOrEmpty(result))
{
//do some code after file updates.
}
}
Now I am getting response but with same old values. I also tried to test it on api console but result is same. I didn't find out the problem.