Windows.Web.Http.HttpClient Authorization header without scheme. - windows-runtime

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

Related

add username and password in post url with basic authentication (Json)

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

Extracting gzip data from Apache-httpclient without decompression

I'm using apache httpclient 4.3.5 to send a request to an upstream server which returns a gzipped response. I need to pass this response AS-IS to a downstream server without any form of decompression. However, httpclient is far too helpful and insists on decompressing the response and I can't find any way of persuading it to stop.
CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse serverResponse = client.execute(serverRequest);
try {
HttpEntity entity = serverResponse.getEntity();
downstreamResponse.setStatus(serverResponse.getStatusLine().getStatusCode());
for (Header header : serverResponse.getAllHeaders()) {
downstreamResponse.setHeader(header.getName(), header.getValue());
}
entity.writeTo(downstreamResponse.getOutputStream());
downstreamResponse.flushBuffer();
} finally {
serverResponse.close();
}
I'm sure that there is some way of configuring the client using some form of the construct
return HttpClients.custom()
....
.build();
but I can't find it. Can the experts please advise?
CloseableHttpClient client = HttpClients.custom()
.disableContentCompression()
.build();

WinRT use HttpClient to call Web API based URL with token

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.

Json Webservice Call in Apex Salesforce

Can anyone share an end to end example for making a JSON webservice Call Through Apex ( Visual Force Pages and Controllers ) in Salesforce .
Pretty Much like we do in HTML5 ,Jquery by Ajax !
There are examples right in the documentation of calling REST web services.
From HTTP Classes:
public class HttpCalloutSample {
// Pass in the endpoint to be used using the string url
public String getContent(String url) {
// Instantiate a new http object
Http h = new Http();
// Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod('GET');
// Send the request, and return a response
HttpResponse res = h.send(req);
return res.getBody();
}
}
You can change the method to one of:
GET, POST, PUT, DELETE, TRACE, CONNECT, HEAD, and OPTIONS
A more complete example is available at HTTP (RESTful) Services
There is also support for JSON deserialization.
Don't forget to use the Remote Site Settings to open up access to the target domain.
For a SOAP web service you can define Apex classes from a WSDL.

Yahoo Mail JSON API Invalid JSON

I am trying to use Java Scribe Library to integrate with Yahoo Web Service. I was able to get the OAuth Integration done.
Now I am trying to call the ListMessages JSON API using sample request in here http://developer.yahoo.com/mail/docs/user_guide/JSON-RPCEndpoint.html#
My code looks like this:
Token requestToken = buildTokenFromDB();
OAuthService service = new ServiceBuilder().provider(YahooApi.class).apiKey(API_KEY).apiSecret(API_SECRET).build();
OAuthRequest request = new OAuthRequest(Verb.GET,
"http://mail.yahooapis.com/ws/mail/v1.1/jsonrpc");
String str = getFilesAsString("msg.json");
request.addPayload(str);
request.addHeader("Content-Type", "application/json");
request.addHeader("Accept", "application/json");
service.signRequest(accessToken, request);
Response response = request.send();
I am getting the following error:
{"result":null,"error":{"code":"Client.InvalidRequest","message":"Invalid Json.","detail":null}}
Looks like I can only use GET, but I am not sure if I need to use some param for payload or scribe does it automatically.
Thanks.
Geeth