Creating a Custom Apache HTTP Client Redirect policy - apache-httpclient-4.x

Need little help, i might sound little confusing..
I am using Apache HTTP Client 4.5.1, I have a post url which needed some header to be used to call, once we call post url it return 303 and a get url, but As per HTTP Client redirect policy its automatically does redirect GET CALL but it send header also in the redirected GET CALL.
How do i override redirect policy of HTTP Client so that 303 redirect does not use header when calling redirect GET URL.
Any idea how to achieve this. I have already checked few option with redirect policy.

The culprit is this line of code in the RedirectExec request execution interceptor.
One should be able to prevent it from copying the original request headers by adding some harmless dummy request header with a custom redirect strategy
CloseableHttpClient client = HttpClients.custom()
.setRedirectStrategy(new DefaultRedirectStrategy() {
#Override
public HttpUriRequest getRedirect(
HttpRequest request,
HttpResponse response,
HttpContext context) throws ProtocolException {
HttpUriRequest redirectRequest = super.getRedirect(request, response, context);
redirectRequest.addHeader("x-custom", "stuff");
return redirectRequest;
}
})
.build();

Related

JavaScript: Cross-Origin Read Blocking (CORB) blocked cross-origin response when request through gitlab api

I have successfully received the user's id through gitlab api, but when I tried to receive the project list of the user, chrome returns CORB.
I have successfully received the id and all other user info with GitLab api v4. But when I was trying to receive the repo list of the user, I received the CORB block and get nothing on the response text. I can see the data in json form if the website address is entered directly. If I use jQuery.getJSON directly, it will ask me to add X-Requested-With or Origin to the header, which I don't know how to achieve.
var target = 'https://gitlab.com/api/v4/users/'+doc.data().gitlab_id+'/projects?callback=?';
console.log(target);
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = process;
xhr.open("GET", target, true);
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.setRequestHeader('responseType', 'text/plain');
xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
xhr.send();
function process() {
console.log(xhr);
I expect to get the json data, but now the response text is empty and it returns a message below:
Cross-Origin Read Blocking (CORB) blocked cross-origin response https://gitlab.com/api/v4/users/USER_ID/projects?callback=jQuery11130057655514217979986_1563975117007&_=1563975117008 with MIME type application/json. See https://www.chromestatus.com/feature/5629709824032768 for more details.
For anyone who faces the same problem:
Try adding 'https://cors-anywhere.herokuapp.com/' right before your request url.
For example, you can use
'https://cors-anywhere.herokuapp.com/https://api.linkedin.com/v2/me?oauth2_access_token=' + access_token;
to get info from linkedin without being troubled by cors policy.

How to make HTTP request to call a report

On .Net side there is System.net and WebClient to make http request; In Angular 6 what's the way to
Add user name password to the header of HTTP request.
Make http request to call a Jasper/SSRS report via url
Display the response in a new browser window.
Thanks

HttpGet method with Http and Https Protocols return response 200 for Https but 404 for Http protocol

Facing a problem with protocols, please find the below code snippet.
If rquestedUrl starts with https, getResponse.getStatusLine().getStatusCode() is returning 200 whereas if the same rquestedUrl starts with http, getResponse.getStatusLine().getStatusCode() is returning 404.
But if I hit through browser I am able to access the requested file in both cases. Could you please let me know what could be the possible problems. (It is used as a filter before calling Orbeon, this is on the process of implementing WSSO.)
System.setProperty("javax.net.ssl.trustStore", "path_to_JKSFile");
System.setProperty("javax.net.ssl.trustStoreType","JKS");
System.setProperty("javax.net.ssl.trustStorePassword","password");
String rquestedUrl = "http://bigminds.web.mindblow.com/project/project1/views/files/Home.xhtml";
HttpClient client = HttpClients.createDefault();
HttpGet get = new HttpGet(rquestedUrl);
get.addHeader("Cookie", "somevalues");
get.addHeader("Host", "bigminds.mindblow.com:14325");
HttpResponse getResponse = client.execute(get);
It's likely that the endpoint you are hitting does not accept http requests, all requests must be though https. So there isn't anything wrong with your code, it's just not set up to properly integrate with the site you have there. Looking at the code it looks like you may need to have some type of session (ie: need to login) with the webserver to get the data you want.

Is there a way to send a Content-Type header in a GET request on Windows Phone 8?

Can I send a Content-Type header in a Windows Phone 8 GET request?
I have to use a web service provided by our client that requires consumers to send a Content-Type header even with GET requests. Is there a way to achieve this?
I've tried using System.Net.Http.HttpClient and System.Net.HttpWebRequest (which is also used by System.Net.WebClient).
HttpClient throws the following exception:
System.InvalidOperationException: Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.
WebClient throw the following exception:
System.Net.WebException: An exception occurred during a WebClient request.
---> System.Net.ProtocolViolationException: A request with this method cannot have a request body.
Actually, I fully agree with the exceptions, but I really need to access the APIs.
For HttpWebRequest you can use
webRequest = (HttpWebRequest)WebRequest.Create(<yourUri>);
webRequest.Method = "GET";
webRequest.ContentType = "application/json";
See http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.contenttype(v=vs.110).aspx for more details (example uses POST) on how to use it with HttpWebRequest.

Cross-domain $http request AngularJS

I have simple website I'm building with AngularJS which calls an API for json data.
However I am getting Cross domain origin problem is there anyway around this to allow for cross domain requests?
Error:
XMLHttpRequest cannot load http://api.nestoria.co.uk/api?country=uk&pretty=1&action=search_listings&place_name=soho&encoding=json&listing_type=rent. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http:// localhost' is therefore not allowed access.
searchByPlaceName: function() {
var url = baseurl+'country=uk&pretty=1&action=search_listings&place_name=london'+encoding+type;
return $http.get(url);
}
It seems that api.nestoria.co.uk does not allow CORS. It has to set the Access-Control-Allow-Origin header itself -- you have no direct control over that.
However, you can use JSONP. That site allows it via the callback query parameter.
$http.jsonp(baseurl+'country=uk&pretty=1&action=search_listings&place_name=london'
+encoding+type + "&callback=JSON_CALLBACK")
Install Fiddler. Add a custom rule to it:
static function OnBeforeResponse(oSession: Session)
{
oSession.oResponse.headers.Add("Access-Control-Allow-Origin", "*");
}
This would allow you to make cross domain requests from localhost. If the API is HTTPS make sure you enable 'decrypt HTTPS traffic' in fiddler.
Reference
-------------------- UPDATE
The response you are getting is JSON. Specifying JSONP as datatype would not work. When you do specify JSONP the return should be a function not JSON object.
JSONP