1

I searched other topics and there were some answers but I didn't succeed to solve my problem. I have this code and I want to add "Referer" to my http headers. After using setRequestProperty method, I log the results in Logcat but I don't see referer in the output. what am I doing wrong?

URL url = new URL(uri); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestProperty("Referer", "http://www.example.com"); for(int i=0;con.getHeaderFieldKey(i)!=null;i++){ String headerName = con.getHeaderFieldKey(i); String headerValue = con.getHeaderField(i); Log.d("Header", headerName + ": " + headerValue); } 

I also have another code which is not working either:

URL url = new URL(uri); HttpURLConnection con = (HttpURLConnection) url.openConnection(); String IRNIC = cookies.get("IRNIC"); String ROUTEID = cookies.get("ROUTEID"); String myCookies = "IRNIC="+IRNIC+"; ROUTEID="+ROUTEID; con.setRequestProperty("Cookie", myCookies); for(int i=0;con.getHeaderFieldKey(i)!=null;i++){ String headerName = con.getHeaderFieldKey(i); String headerValue = con.getHeaderField(i); Log.d("Header", headerName + ": " + headerValue); } 

For the first code, I don't see referer in the output and also for the second code, I don't see cookies too. So it seems setRequestProperty is not working! Thanks in advance.

EDIT: I can see headers in the output but not the ones I added via setRequestProperty method. so the if code is working.

1 Answer 1

1

From docs:

getHeaderFields

Returns an unmodifiable map of the response-header fields and values

setRequestProperty

Sets the value of the specified request header field.

Request is not the same as response. That's why your headers are different. The request will have correct headers using setRequestProperty

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.