Tag: httprequest

无法使用restTemplate发送请求,与postman应用程序中的请求相同

我有来自Postman的http模板: 要使用java代码执行相同的请求,我编写了以下代码: LinkedMultiValueMap map = new LinkedMultiValueMap(); exchange.getIn().getHeader(Exchange.FILE_NAME_ONLY, String.class); map.add(“file”, new File(“filePath”); int lastIndexOfDot = fileName.lastIndexOf(“.”); map.add(“type”, fileName.substring(lastIndexOfDot + 1)); map.add(“org_id”, systemSettingsService.getSystemSettings().getOrganizationId()); map.add(“stone_id”, fileName.substring(0, lastIndexOfDot)); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.MULTIPART_FORM_DATA); HttpEntity<LinkedMultiValueMap> requestEntity = new HttpEntity( map, headers); restTemplate = new RestTemplate(); try { ResponseEntity result = restTemplate.exchange(buildUrl(), HttpMethod.POST, requestEntity, String.class); logger.info(“result {}”, result); } […]

检查有效会话:isRequestedSessionIdValid()vs getSession(false)

我正在开发Java Servlets。 在检查用户是否已登录时,我想检查HTTP请求是否具有有效会话。 为了检查,我有两种可能性: (1) protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(false); if (session != null) { // user is logged in … } } 由于我将false作为参数传递,如果没有有效的会话已经存在,则没有创建新的会话,并且该函数返回null,我可以检查。 或者我这样做: (2) if (request.isRequestedSessionIdValid()) { // user is logged in … } 有什么区别,有什么优势/劣势吗? 或者这两个function或多或少相同?

如何使用指数退避实现HttpRequestRetryHandler?

我希望在第一次请求失败的情况下为HttpClient实现HttpRequestRetryHandler 。 我还希望为后续重试实施指数退避 。 在数学上它可以实现为 E(c)= 1/2(2次幂(c)-1) 但是我现在很难在HttpRequestHtryHandler的代码中实现它。

如何在下一个活动中显示响应

在我的应用程序中,我正在搜索模块我之前的问题是如何在其他活动中显示json响应? 因为我在服务器发送请求,然后我得到响应,作为回应我得到一些数据,我希望在下一页显示的数据,我不知道该怎么做,任何人都可以帮助吗? class AttemptLogin extends AsyncTask { boolean failure = false; @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(getActivity()); pDialog.setMessage(“Processing..”); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.show(); } @SuppressWarnings(“unused”) @Override protected String doInBackground(String…args) { //Check for success tag //int success; Looper.prepare(); String userids = strtext.toString(); String contri=spcountry.getText().toString(); String states=spstate.getText().toString(); String city=spcity.getText().toString(); System.out.println(“Email : ” + userids); System.out.println(“Email […]

从套接字读取时如何检测客户端何时完成发送请求?

我现在正在写一个http服务器,我从sockets读取时遇到问题。 我的问题是来自客户端的inputStream永远不会结束,它会一直读取,直到客户端关闭。 我知道客户端在发送http请求后不会立即关闭与服务器的连接。 当客户端发送了所有请求数据(即标题+正文)时,如何退出while loop 。 while (in.hasNextLine()) { String line = in.nextLine(); if (line.equals(“”)){ // last line of request header is blank break; // quit while loop when last line of header is reached } else { request = request + line + “\n”; } } 在阅读了你们的评论和回答之后,这就是我想出来的, is = incoming.getInputStream(); os = incoming.getOutputStream(); in […]

如何在HttpRequest中获取源文件以上传文件

我在使用java的REST Web服务中上传文件时遇到问题。 我不知道如何获取源文件。 curl命令就是这样 curl –upload-file /home/student1/text1.txt http:// localhost:20260 / project / resources / user1 / sub-folder / text1.txt 我的问题是,任何人都知道如何在REST Web服务@PUT方法中获取源文件URL“/home/student1/text1.txt”?还是任何其他方式来获取它? 谢谢

在Java中的HTTP请求中添加HEADER

我正在使用以下代码发送简单的HTTP请求: try { Socket s = new Socket (); s.bind (new InetSocketAddress (ipFrom, 0)); s.connect (new InetSocketAddress (ipTo, 80), 1000); PrintWriter writer = new PrintWriter (s.getOutputStream ()); BufferedReader reader = new BufferedReader (new InputStreamReader (s.getInputStream ())); writer.print (“GET ” + szUrl + ” HTTP/1.0\r\n\r\n”); writer.flush (); s .close (); reader.close (); writer.close (); } 但是,正如您所看到的,我不发送自定义HEADER。 […]

什么是apache中的SchemeRegistry以及何时应该使用它?

Apache HTTPClient包含SchemeRegistry类(org.apache.http.conn.scheme.SchemeRegistry) 什么是Scheme Registry? 什么时候可以使用? 如果我使用如下所示的方案注册表,它会产生什么影响 SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme(WEBSERVICE_URI_SCHEME, 80 ,PlainSocketFactory.getSocketFactory())); PoolingClientConnectionManager wsConnManager = new PoolingClientConnectionManager (registry);

如何从HttpServletRequest只获取部分URL?

从以下URL我需要单独获取(http://localhost:9090/dts) 。 那是我需要删除(documents/savedoc) (OR) 需要得到 – (http://localhost:9090/dts) http://localhost:9090/dts/documents/savedoc 是否有任何方法可用于获得上述内容? 我尝试了以下并得到了结果。 但仍在努力。 System.out.println(“URL****************”+request.getRequestURL().toString()); System.out.println(“URI****************”+request.getRequestURI().toString()); System.out.println(“ContextPath****************”+request.getContextPath().toString()); URL****************http://localhost:9090/dts/documents/savedoc URI****************/dts/documents/savedoc ContextPath****************/dts 任何人都可以帮我解决这个问题吗?

在servlet中获取请求url的一部分

我有一个使用/ev/* url-pattern映射的EmailVerification Servlet。 http://example.com/ev/ce52320570 如何在我的Servlet中获取此ce52320570部分URL? protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String vid = “”; // Here I need to get the id from the URL }