4

I wrote php curl to request some data from my API server, and now I want to develop a JSP to request the data from the same API server.

The problem comes when my API server need http referer to check if the request come from a valid url.

My original PHP is:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/2');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.example.com/1');
$html = curl_exec($ch);

... then my api server get the referer url with

var_dump($_SERVER['HTTP_REFERER'])

How do I use Apache HttpComponents HttpClient to set the Referer header?

4
  • Are you asking 'how can I set the HTTP Referer header using a Java HTTP library?' Commented Aug 12, 2015 at 4:12
  • that is what i want .. i just don't know how to describe it .. and can send it to my api server Commented Aug 12, 2015 at 4:13
  • Which Java HTTP client library are use using? Commented Aug 12, 2015 at 4:13
  • i use Apache HttpComponents HttpClient Commented Aug 12, 2015 at 4:18

1 Answer 1

4
HttpGet request = new HttpGet("http://www.example.com/2")

request.addHeader("Referer", "http://www.example.com/1")

HttpResponse response = HttpClientBuilder.create().build().execute(request)

You can set header by using AbstractHttpMessage addHeader

Sign up to request clarification or add additional context in comments.

2 Comments

I believe the header as transmitted on the wire is called Referer, not HTTP_REFERER.
@GregKopff Yeah, Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.