【如何告诉Java对HTTP请求使用特定的传出IP接口?】教程文章相关的互联网学习教程文章

java – 用于处理方向更改的http请求的AsyncTaskLoader,使用泛型,继承【代码】

目前我正在使用异步http库来对我们的服务器执行http请求.然而,这带来了一个问题,即如果在屏幕旋转期间正在进行http调用,我们将在调用结束时引用旧的上下文.我通过保持对onCreate中捕获的最新实例的静态引用来解决这个问题,并使用该引用调用方法(并在onDestroy中将其置空).它运作正常但似乎是一个黑客.我见过有人推荐使用片段来解决这个问题,比如这里: http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-con...

当HTTP请求具有返回状态401时,如何在Java中解析响应主体【代码】

我正在使用Spring的RestTemplate和Jackson来使用RESTful JSON API.在某些情况下,我们可能会收到一个状态401(未授权)响应,其中包含由API制造商定义的自定义JSON主体,如下所示:{"code": 123,"message": "Reason for the error" }我们需要解析主体,并在业务逻辑中使用code属性. 这是我们需要解析的错误响应Java对象:public class CustomError {@JsonPropertyprivate Integer code;@JsonPropertyprivate String message;public Integ...

Spring Boot,Java Config – 在DispatcherServlet中找不到带有URI [/ …]的HTTP请求的映射,名称为“dispatcherServlet”【代码】

这在stackOverflow中是一个非常常见的问题,但同一问题的主题都没有解决我的问题. 我们有一个使用xml配置的模板配置,但现在我们正试图摆脱它并开始使用Java配置. 所以我有一个使用Java配置和Spring Boot的新项目.我们也使用JSP和Tiles 3. 问题是:它无法呈现我们的管理员登录页面. 这是代码: 主配置类:@SpringBootApplication @EnableScheduling @Import(OnAdminBeans.class) public class AppConfig extends SpringBootServletIn...

如何将带有“-i -upload-file”的curl调用转换为java Unirest或任何其他http请求?【代码】

以下示例使用cURL上载作为二进制文件包含的图像文件.curl -i --upload-file /path/to/image.png --header "Authorization: Token" 'https://url....' 它工作正常.我需要从我的Java应用程序发出这个请求. 我试过下一个代码URL image_url = Thread.currentThread().getContextClassLoader().getResource("jobs_image.jpg"); String path = image_url.getFile(); HttpResponse<String> response = Unirest.post(uploadUrl).header("ca...

获取java.io.IOException:HTTP请求失败,HTTP状态:在ksoap2中404,同时将xml数据传递给soap1.2 android【代码】

我必须通过<?xml version='1.0' encoding='utf-8' ?> <hello><username>test@test.com</username> <password>test</password></hello>至 WSDL<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" ...

java – Tomcat 7.0.43“INFO:解析HTTP请求标头时出错”【代码】

我使用带有websocket应用程序的Tomcat 7.0.43.我的应用程序在Tomcat 7.0.42中工作正常,但是当我尝试在websockets上访问我的服务器时,我得到以下输出:Sep 16, 2013 3:08:34 AM org.apache.coyote.http11.AbstractHttp11Processor process INFO: Error parsing HTTP request headerNote: further occurrences of HTTP header parsing errors will be logged at DEBUG level.我的浏览器控制台显示以下内容WebSocket connection to 'w...

JAVA http请求

/** * */ package com.app.record; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import org.apache.commons.io.IOUtils; /** * @author Administrator * @date 2016-11-05 02:39:04 * */ public class sendMagToRecWhenCreate { public static String doHttpPost(String touserId, String b) { ...

Java -- 通过 URLConnection 进行http请求中文乱码【代码】

对writer和reader指定字符集 out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "utf-8")); in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8")); 具体代码: public static String sendPost(String url, String param) {PrintWriter out = null;BufferedReader in = null;String result = "";try {URL realUrl = new URL(url);// 打开和URL之间的连接URLConnection conn = ...

java – Spring Boot添加Http请求拦截器【代码】

在Spring启动应用程序中添加HttpRequest拦截器的正确方法是什么?我想要做的是为每个http请求记录请求和响应. Spring启动文档根本不涉及此主题. (http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/) 我发现了一些关于如何对旧版本的spring执行相同操作的Web示例,但这些示例与applicationcontext.xml一起使用.请帮忙.解决方法:由于您使用的是Spring Boot,我认为您更愿意尽可能依赖Spring的自动配置.要添加其他...

java – 为什么Spring MVC用404响应并报告“在DispatcherServlet中找不到带有URI […]的HTTP请求的映射”?【代码】

我正在编写一个部署在Tomcat上的Spring MVC应用程序.请参阅以下minimal, complete, and verifiable examplepublic class Application extends AbstractAnnotationConfigDispatcherServletInitializer {protected Class<?>[] getRootConfigClasses() {return new Class<?>[] { };}protected Class<?>[] getServletConfigClasses() {return new Class<?>[] { SpringServletConfig.class };}protected String[] getServletMappings() ...

java – 如何确保http请求来自特定用户?

我正在为网站编写一个附加服务,该服务利用现有的登录行为,同时需要用户提供一些额外的注册详细信息.新服务在不同的子域中运行. 用户将能够在我的数据Web应用程序上创建资源,必须根据该用户的数据集保存资源. 我希望这个用户标识符将传递给http请求正文中的webapp.但是,我担心恶意攻击可能会重写正文中的用户名,使请求看起来像是来自其他用户. 我该怎么做才能让这更安全? (这是否算作CSRF攻击?) 新服务是用Java编写的,Spring 3.解...

java – 从HTTP请求获取完整路径

我想知道如何获得HTTP请求的完整路径. 如果有一个请求,如http:// localhost:8080 / path1 / path2,我如何获得完整的/ path1 / path2? 使用request.getContextPath()仅返回/ path1部分.解决方法:你需要使用the getRequestURI() method: 文档描述了它:Returns the part of this request’s URL from the protocol name up to the query string in the first line of the HTTP request.

java – 获取完整的HTTP请求【代码】

对于调试问题,我想在提供一组选项时获取Unirest-Java执行的请求.我怎么能得到这个:POST / HTTP/1.1 Host: www.some.host.tld Connection: keep-alive Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 field1=FIELD1&field2=FIELD2由此:HttpRequest request = Unirest.post(SOMEHOST_URL).field("field1" , FIELD1).field("field2", FIELD2).getHttpRequest();也就是说,如何从HttpRequest获...

java使用httpclient发送HTTPS请求【代码】

读前须知 我会把需要依赖的包和引入的包先贴给大家,防止大家引用错误。 <dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpcore</artifactId><version>4.4.8</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.3</version></dependency><dependency><groupId>org.testng</groupId><artifactId>testng</artifactId...

java – HTTPS请求和多线程

Java的URL类是线程安全的,特别是[URL.openConnection()](http://java.sun.com/javase/6/docs/api/java/net/URL.html#openConnection())? 在我的应用程序中,我在同一个URL上创建了数十个并发HTTPS连接,我希望最大化对象重用.然而,从文档中可以重用的不清楚. 编辑:如果需要,我愿意使用不同的库.解决方法:我对HTTP和java的标准响应是推荐Apache HttpClient.它支持HTTP 1.1,因此您可以在服务器成功获得HTTP请求/响应后保持这些连接打...