How to add the header in the WebRequest .
HttpWebRequest tRequest = (HttpWebRequest)WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "POST";
tRequest.ContentType = "application/json";
var data = new
{
to = devicesId,
notification = new
{
body = "Fcm Test Notification",
title = "Test FCM",
sound = "Enabled"
},
priority = "high"
};
tRequest.Headers["Authorization: key={0}"] = appId;
tRequest.Headers["Sender: id={0}"] = senderId;
i need to add the header to create the web Request.
Thanks
Missing Add in HttpWebRequest is because have missing ISerializable in CoreFx (so you can not simply tRequest.Headers.Add("name", "value");). They working on it and you can follow at https://github.com/dotnet/corefx/issues/12669
Related
I am trying to get AngleSharp to use both a proxy and set header properties like this:
var handler = new HttpClientHandler
{
Proxy = new WebProxy(ProxyMan.GetProxy()),
UseProxy = true,
PreAuthenticate = true,
UseDefaultCredentials = false
};
var requester = new DefaultHttpRequester();
requester.Headers["User-Agent"] = Tools.GetAgentString();
requester.Headers["Accept-Language"] = "en-US";
requester.Headers["Accept-Charset"] = "ISO-8859-1";
requester.Headers["Content-Type"] = "text/html; charset=UTF-8";
var config = Configuration.Default
.WithRequesters(handler)
.With(requester)
.WithTemporaryCookies()
.WithDefaultLoader();
var context = BrowsingContext.New(config);
var doc = await context.OpenAsync(Url);
When I added the Header requester, it stopped the proxy handler from working. I know there is some conflict between the .WithRequesters() and the .With() but I cannot locate the proper syntax for doing both in the same request.
Thanks.
Yeah I am not sure why you use both.
The DefaultHttpRequester is the requester from AngleSharp and uses WebClient underneath
WithRequesters comes from AngleSharp.Io and adds all available requesters (incl. HttpClientRequester)
WithDefaultLoader puts a loader into the config and registers the default HTTP requester, if no other requester has been registered yet
I think what you want to do should be actually done using the HttpClientRequester directly.
var handler = new HttpClientHandler
{
Proxy = new WebProxy(ProxyMan.GetProxy()),
UseProxy = true,
PreAuthenticate = true,
UseDefaultCredentials = false,
UseCookies = false,
AllowAutoRedirect = false
};
var client = new HttpClient(handler);
client.DefaultRequestHeaders.Append("User-Agent", Tools.GetAgentString());
// ... and the others if you want, even though `content-type` etc. should / will be determined by AngleSharp
var config = Configuration.Default
.With(new HttpClientRequester(client))
.WithTemporaryCookies()
.WithDefaultLoader();
var context = BrowsingContext.New(config);
var doc = await context.OpenAsync(Url);
I'm trying to send some data using the example in the page of onesignal
var request = WebRequest.Create("https://onesignal.com/api/v1/notifications") as HttpWebRequest;
request.KeepAlive = true;
request.Method = "POST";
request.ContentType = "application/json; charset=utf-8";
request.Headers.Add("authorization", "Basic xxx");
var obj = new
{
app_id = "xxx",
contents = new { en = "English Message" },
included_segments = new string[] { "Active Users" }
};
var param = JsonConvert.SerializeObject(obj);
byte[] byteArray = Encoding.UTF8.GetBytes(param);
This coded works fine, but I'm using Flurl to make a request to onesignal like this:
var body = new
{
app_id = "xxx",
contents = new
{
es = "Mensaje prueba"
},
included_segments = new string[] { "All" }
};
string param = JsonConvert.SerializeObject(body);
var content = new System.Net.Http.ByteArrayContent(Encoding.UTF8.GetBytes(param));
var response = await new Flurl.Url(urlbase)
.AppendPathSegment("notifications")
.WithHeader("Content-Type", "application/json; charset=utf-8")
.WithHeader("Authorization", "Basic xxx")
.PostAsync(content)
.ReceiveString();
but I'm getting the "Bad request". Please someone could help to point how to make the same call with Flurl?
As mentioned in the first comment, you're doing more work than you need to. Flurl will serialize body for you, so remove these lines:
string param = JsonConvert.SerializeObject(body);
var content = new System.Net.Http.ByteArrayContent(Encoding.UTF8.GetBytes(param));
And post body directly using PostJsonAsync:
var response = await urlbase
...
.PostJsonAsync(body)
.ReceiveString();
I have connect my phone to my localhost(xampp) but i fail to save the picture take by xamarin plugin to my pc directory. Below is my code:
public MainPage()
{
InitializeComponent();
//CameraButton.Clicked += CameraButton_Clicked;
var request = new HttpRequestMessage();
request.RequestUri = new Uri("http://192.168.137.1/pic/");
request.Method = HttpMethod.Post;
request.Headers.Add("Accept", "application/json");
//var client = new HttpClient();
//HttpResponseMessage response = client.SendAsync(request);
}
Below is save photo code:
var file = CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
DefaultCamera = Plugin.Media.Abstractions.CameraDevice.Rear,
CompressionQuality = 92,
SaveToAlbum = false,
Directory= "http://192.168.137.1/pic/",
Name = DateTime.Now + "_test.jpg"
});
Can anyone share me idea how to make it done? Please help and thanks.
So i am trying to integrate PayPal in my Flex Mobile app. I make my first call like this:
(keys are sandbox by paypal dev resources)
protected function getPaypal():void {
var client_id:String="EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp";
var secret:String="EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp";
var params:Object = new Object();
params.grant_type="client_credentials";
var encoder:Base64Encoder = new Base64Encoder();
encoder.encode(client_id + ":" + secret);
//var s:String = JSON.stringify(params);
paypal.contentType = "application/x-www-form-urlencoded";
paypal.headers["Authorization"] = "Basic " + encoder.toString();
paypal.method = "POST";
paypal.url = "https://api.sandbox.paypal.com/v1/oauth2/token";
paypal.send(params);
}
This fails and returns the following:
'Error #2096: The HTTP request header Basic RU9KMlMtWjZPb05fbGVfS1MxZDc1d3NaNnkwU0ZkVnNZOTE4M0l2eEZ5WnA6RUNsdXNNRVVrOGU5
aWhJN1pkVkxGNWNaNnkwU0ZkVnNZOTE4M0l2eEZ5WnA= cannot be set via ActionScript.' faultDetail:'null'
I can't figure out what seems to be the problem.
Any help?
Maybe this?
https://stackoverflow.com/a/539173/3384609
Gist:
You can fix this by setting (in the above example)
encoder.insertNewLines = false; The default setting is true.
I need to send a byte array of data as a HTTP body using HTTPService class in the Adobe Air API. Can anyone suggest me the way of doing this?
Try this
var encodedString : String = Base64.encode( imageByteArray );
var service : HTTPService = new HTTPService();
service.method = "POST";
service.contentType = 'application/x-www-form-urlencoded';
service.url = 'http://www.mydomain.com/upload.php';
var variables : URLVariables = new URLVariables();
variables.imageArray = encodedString;
variables.variable2 = "some text string";
variables.variable3 = "some more text";
service.send( variables );
Base64 class get from
http://code.google.com/p/jpauclair-blog/source/browse/trunk/Experiment/Base64/src/Base64.as