HttpURLconnection添加请求头
时间: 2023-10-10 15:13:15 浏览: 140
要在HttpURLConnection中添加请求头,可以使用setRequestProperty方法。例如,可以使用以下代码设置content-type和token请求头:
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Content-type", "application/json");
connection.setRequestProperty("token", "10051:abc");
另外,对于HttpPost请求,可以使用addHeader方法添加请求头。例如:
HttpPost httppost = new HttpPost(url);
httppost.addHeader("Authorization", "your token");
httppost.addHeader("Content-Type", "application/json");
httppost.addHeader("User-Agent", "imgfornote");
这样就可以向HttpURLConnection或HttpPost请求中添加请求头了。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [HTTP post和URLConnection连接设置头信息](https://blog.csdn.net/xiangyihu/article/details/78327225)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *3* [JAVA:使用HttpURLConnection添加头部header信息并进行httpget请求](https://blog.csdn.net/quan278905570/article/details/119798731)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文