I want to add username and password in post url with basic authentication
http://IPaddress:port/F1/Details
{
Body
}
Your not specified the programming language but if you are using c# it will be like this:
HttpClient client = new HttpClient();
var byteArray = Encoding.ASCII.GetBytes("username:password");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
client.PostAsync("http://IPaddress:port/F1/Details", yourcontent);
Using Postman
Related
I am working on customizing my spring boot authorization server. After login using username and password from the html page I have created I need to redirect back to /oauth/token endpoint ,in order to get the access token .
It is working fine while using postman. But when I give defaultsuccessurl as /ouath/token it shows me a default login page to enter username and password which the basic auth username and password from postman.
so, in order to get the access token from front end I need to add basic auth details as http header. I don't know where to add HTTP header and how to use that to get the access token after successful login using front end.
Any help appreciated
There are multiple ways to do it. One way is: along with html, you can use Javascript to set HttpHeaders and send the request. Below a sample code snippet (not complete though)
var user = "user1"; //ideally this should come from Input field in html page
var password = "MySecret";//ideally this should come from password field in html page
// var authorizationBasic = $.base64.btoa(user + ':' + password);
var authorizationBasic = window.btoa(user + ':' + password);
var request = new XMLHttpRequest();
request.open('POST', oAuth.AuthorizationServer, true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
request.setRequestHeader('Authorization', 'Basic ' + authorizationBasic);
request.setRequestHeader('Accept', 'application/json');
request.send("<requestURL>");
I don't know where to look or what to check but ask my question in title. Xamarin forms app works when sending json data over virtual phone instance but doesn't send data over physical no matter which platform i use , it is the same with iOS and Android.
static async Task phoneInfo()
{
string url = "http://blabla.com/api/blabla";
string sContentType = "application/json";
JObject jsonObject = new JObject();
jsonObject.Add("DeviceModel", DeviceInfo.Model);
jsonObject.Add("DeviceManufacturer", DeviceInfo.Manufacturer);
jsonObject.Add("DeviceName", DeviceInfo.Name);
jsonObject.Add("DeviceVersion", DeviceInfo.VersionString);
jsonObject.Add("DevicePlatform", DeviceInfo.Platform);
jsonObject.Add("DeviceIdiom", DeviceInfo.Idiom);
jsonObject.Add("DeviceType", DeviceInfo.DeviceType.ToString());
jsonObject.Add("AreaOne", DateTime.UtcNow.ToString());
jsonObject.Add("Deleted", false);
HttpClient oHttpClient = new HttpClient();
var oTaskPostAsync = await oHttpClient.PostAsync(url, new StringContent(jsonObject.ToString(), Encoding.UTF8, sContentType));
}
usage is simple like code. just put await phoneInfo(); where i want to take info.
i have accesswifistate and internet permission over Android and NSAppTransportSecurity for non https connection with iOS.
Any ideas where am i doing wrong?
I want to send and receive JSON over TCP.
QUESTION: I have to send and receive JSON in my TCP client-server. How can I achieve it?
I use TcpListener and TcpClient to connect and I have this code:
NetworkStream stream = client.GetStream();
var serializer = new JsonSerializer();
var sr = new StreamReader(stream, new UTF8Encoding(), false);
var jsonTextReader = new JsonTextReader(sr);
var data = serializer.Deserialize(jsonTextReader).ToString();
Console.WriteLine("Received: {0}", data);
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
JsonWriter writer = new JsonTextWriter(sw);
writer.WriteValue('1');
byte[] buffer = Encoding.ASCII.GetBytes(writer.ToString());
stream.Write(buffer, 0, buffer.Length);
Can I do it better? The client has to receive JSON(I use Newtonsoft.Json) and I don't know if it is even good code. Maybe you write me some good practices? Or maybe some tips.
EDIT.
Now I wrote something like this:
public static T DeserializeFromStream<T>(Stream stream)
{
using (var sr = new StreamReader(stream))
using (var jsonTextReader = new JsonTextReader(sr))
{
return new JsonSerializer().Deserialize<T>(jsonTextReader);
}
}
And it doesn't work because Java client send me array like: [{"name" : "logo", "session" : "i3fnj34njn780"}] So how can I fix this problem? I want call it this way: Method ar = DeserializeFromStream<Method>(client.GetStream()); Trim and Replace doesn't work for me here.
byte[] buffer = Encoding.ASCII.GetBytes(writer.ToString());
This is incorrect. Per RFC4627:
Encoding
JSON text SHALL be encoded in Unicode. The default encoding is
UTF-8.
You are not sending JSON.
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
JsonWriter writer = new JsonTextWriter(sw);
This is unnecessarily writing a SB. Write directly into the network stream:
using (NetworkStream stream = client.GetStream()) {
using (TextWriter tw = new StreamWriter(stream, Encoding.UTF8)) {
using (JsonWriter writer = new JsonTextWriter(tw)) {
...
}
}
}
Also, use using.
Plus, everything #CodeCaster says. This should be a proper Web API, not some rogue TCP server. Not only the obvious issue of having more than one request type (ie. routing), but you have to consider proxies (none will allow some arbitrary port), server authentication (you must tunnel through HTTPS and validate the server cert in the Android APP), make allowance for web caching, HTTP headers etc etc. And you need proper error states and error codes for your 'protocol', which HTTP provides out-of-the-box. And a good job would be to model a proper REST API, and likely a good data model on top of JSON, like JSON-API.
I have a winrt app and a Windows.Web.Http.HttpClient
I want to set its Authorization header without using a scheme. My code is as below.
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new HttpCredentialsHeaderValue("Scheme", "mytoken");
This will result in this
Authorization: Scheme mytoken
What I want is this
Authorization: mytoken
The problem is that the Constuctor of HttpCredentialsHeaderValue has to take a scheme argument and that scheme cannot be String.empty
Is there a way I can achieve this result?
Try:
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.TryAppendWithoutValidation(
"Authorization",
"mytoken");
We are building a WinRT app which gets data from server which is Web API based & so it gives data in json and/or XML format.
When app user logs in for the first time using his credentials(username,password), the response that comes from server is a success bit & a TOKEN, which should be used in successive URL requests.
I am using httpclient for sending requests
using (HttpClient httpClient1 = new HttpClient())
{
string url = "http://example.com/abc/api/process1/GetLatestdata/10962f61-4865-4e7a-a121-3fdd968824b5?employeeid=6";
//The string 10962f61-4865-4e7a-a121-3fdd968824b5 is the token sent by the server
var response = await httpClient1.GetAsync(new Uri(url));
string content = await response.Content.ReadAsStringAsync();
}
Now the response that i get is with status code 401 "unauthorised".
And the xml i get in response is "Unauthorised User".
Is there anything i need to change in appManifest??
I've checked this, but cant we use httpclient without credentials??
Your Capabilities are enough. You don't even need Internet (Client) because it's included in Internet (Client & Server).
You do not have credentials for WinRT HttpClient, in your linked post they referr to System.Net.Http.HttpClientHandler.
Maybe you can use the HttpBaseProtocolFilter to add the credentials?
using (var httpFilter = new HttpBaseProtocolFilter())
{
using (var httpClient = new HttpClient(httpFilter))
{
httpFilter.ServerCredential...
}
}
I don't know your security mechanism, I'm using a HttpClient and my session-key is in a cookie. But I think your client code looks fine.