Modify HTTP headers in Google App Engine Endpoints (Android)

跟風遠走 提交于 2019-11-29 23:03:26

问题


I would like to add custom headers to the HTTP package sent to an Endpoint. I've read that I can access the raw HTTP data in the Endpoint method (by adding a HttpServletRequest parameter). What I am looking for is a solution (or a hack) to access and modify the data being sent by the (Android) client.


回答1:


Http headers can be modified like this. I use this link as example. Since it's one of the Google's example.

Read it and find EndpointsAsyncTask you can use GoogleClientRequestInitializer for this. inside

public void initialize(AbstractGoogleClientRequest abstractGoogleClientRequest) throws IOException

you can set headers like this.

HttpHeaders headers = abstractGoogleClientRequest.getRequestHeaders();
headers.setDate(dateFormat.format(calendar.getTime()));
headers.set("MyCustomHeader", "HeaderValue");
abstractGoogleClientRequest.setRequestHeaders(headers);

In above code setDate() can be used to set Date header and set() method has been used to set my own header that can be read from server side. I have used calendar class to get date but Calendar class initialization is not shown here.

I have also write a note on my blog to show how it's actually reside in the method. if you like read that too.



来源:https://stackoverflow.com/questions/22532527/modify-http-headers-in-google-app-engine-endpoints-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!