NTLM Authentication implementation error in wp7 - c#

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

Related

C# Windows Forms Web Api authentication

I have a problem with accessing data from this web site.
Here is my code:
public static string CallRestMethod(string url)
{
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
webrequest.Method = "GET";
webrequest.ContentType = "application/x-www-form-urlencoded";
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
StreamReader responseStream = new StreamReader(webresponse.GetResponseStream(),enc);
string result = string.Empty;
result = responseStream.ReadToEnd();
webresponse.Close();
return result;
}
What should I add to authenticate to this token:
'X-Auth-Token: YOUR_API_TOKEN';

Xamarin Forms Refuse connection a call WS

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);
}
}

Soap Web request is failing

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

Send json data to remote server for authentication

I want to post authentication details to a remote server, sending username, database name and password. My server is running on Ubuntu. But i get error Invalid JSON data: ''
"POST / HTTP/1.1" 400 -
I am new to working on this platform, help me with where i am doing it wrong. Below is my code:
private void SendDataButton_Click(object sender, RoutedEventArgs e)
{
string url = "http://200.84.100.211:9875";
string call = url + "/web/session/authenticate";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(call) as HttpWebRequest;
request.ContentType = "application/json";
request.Method = "POST";
request.BeginGetResponse(new AsyncCallback(SendData), request);
}
void SendData(IAsyncResult callbackResult)
{
HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
Stream postStream = myRequest.EndGetRequestStream(callbackResult);
string postData = "{'db':'demo_shruti','login':'admin','password':'admin'}";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
myRequest.BeginGetResponse(new AsyncCallback(FinishWebRequest), myRequest);
}
void FinishWebRequest(IAsyncResult callbackResult)
{
HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
string responseString = "";
Stream streamResponse = response.GetResponseStream();
StreamReader reader = new StreamReader(streamResponse);
responseString = reader.ReadToEnd();
streamResponse.Close();
reader.Close();
response.Close();
string result = responseString;
}

Http POST in wp8

As I have gone through the examples,I have come across only asynchronous http web request having callback methods,like:-
private void getList(string restApiPath, Dictionary<string, string> output, Type type, string label)
{
webRequest = (HttpWebRequest)WebRequest.Create(restApiPath);
path = restApiPath;
labelValue = label;
webRequest.Method = "POST";
webRequest.ContentType = Globals.POST_CONTENT_TYPE;
webRequest.Headers["st"] = MPFPConstants.serviceType;
webRequest.BeginGetRequestStream(result =>
{
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
// End the stream request operation
Stream postStream = request.EndGetRequestStream(result);
// Create the post data
string reqData = Utils.getStringFromList(output);
string encode = RESTApi.encodeForTokenRequest(reqData);
byte[] byteArray = Encoding.UTF8.GetBytes(encode);
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
request.BeginGetResponse(new AsyncCallback(GetResponseCallbackForSpecificConditions), request);
}, webRequest);
}
private void GetResponseCallbackForSpecificConditions(IAsyncResult ar)
{
//code
}
Kindly Suggest me if we can make a synchronous httpwebrequest for wp8?
Why not try this, it works in a Desktop app:
using (Stream TextRequestStream = UsedWebRequest.GetRequestStream())
{
TextRequestStream.Write(ByteArray, 0, ByteArray.Length);
TextRequestStream.Flush();
}
HttpWebResponse TokenWebResponse = (HttpWebResponse)UsedWebRequest.GetResponse();
Stream ResponseStream = TokenWebResponse.GetResponseStream();
StreamReader ResponseStreamReader = new StreamReader(ResponseStream);
string Response = ResponseStreamReader.ReadToEnd();
ResponseStreamReader.Close();
ResponseStream.Close();
Why not try restsharp?
sample POST code looks like,
RestClient _authClient = new RestClient("https://sample.com/account/login");
RestRequest _credentials = new RestRequest(Method.POST);
_credentials.AddCookie(_cookie[0].Name, _cookie[0].Value);
_credentials.AddParameter("userLogin", _username, ParameterType.GetOrPost);
_credentials.AddParameter("userPassword", _password, ParameterType.GetOrPost);
_credentials.AddParameter("submit", "", ParameterType.GetOrPost);
RestResponse _credentialResponse = (RestResponse)_authClient.Execute(_credentials);
Console.WriteLine("Authentication phase Uri : " + _credentialResponse.ResponseUri);

Categories

Resources