http帖子不是从代码工作,而是从邮递员工作 [英] http post not working from code but working from postman
问题描述
这是代码
public class PostRequestMain {
private static String ReadFile(String path) {
String entityBody = new String();
FileReader file;
try {
file = new FileReader(path);
try {
entityBody = IOUtils.toString(file);
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
return entityBody;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
Map map = new HashMap<String, String>();
map.put("Content-Type", "text/plain");
map.put("userName", "username");
map.put("password", "pass");
String payLoad = ReadFile("src/main/resources/SamplePayload.html");
headers.setAll(map);
HttpEntity<String> requestPayload = new HttpEntity<String>(payLoad,headers);
String url = "http://domain/path";
ResponseEntity<String> responseEntity = null;
UriComponentsBuilder uriComponents = UriComponentsBuilder.fromHttpUrl(url)
.queryParam("param1", "value1");
try {
responseEntity = new RestTemplate().postForEntity(uriComponents.build().encode().toUri(), requestPayload, String.class);
} catch (HttpStatusCodeException ex) {
System.out.println("Status code = " + ex.getStatusCode());
}
String entityResponse = responseEntity.getBody();
System.out.println(entityResponse);
}
}
当我运行上面的代码时,它给出了我异常
when I run above code it gives me exception
线程main中的异常
org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://domain/path ":Connection reset; nested exception is java.net.SocketException: Connection reset
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:503)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:465)
at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:342)
at sprinthttp1.PostRequestMain.main(PostRequestMain.java:61)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:209)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:704)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:647)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1536)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at org.springframework.http.client.SimpleClientHttpResponse.getRawStatusCode(SimpleClientHttpResponse.java:47)
at org.springframework.http.client.AbstractClientHttpResponse.getStatusCode(AbstractClientHttpResponse.java:32)
at org.springframework.web.client.DefaultResponseErrorHandler.getHttpStatusCode(DefaultResponseErrorHandler.java:55)
at org.springframework.web.client.DefaultResponseErrorHandler.hasError(DefaultResponseErrorHandler.java:49)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:489)
... 3 more
但是当我尝试使用Postman向相同的URL发布帖子请求时,我得到了正确的结果。
我尝试了不同的方式发布请求,但每次都出现同样的错误。
任何想法可能会出现什么问题?
But when I try make post request to same URL with Postman I get proper results. I tried different ways to post request but every time same error. Any ideas what could be issue?
推荐答案
没有查看拒绝连接的域的具体示例 - 很难确切地说出了什么问题。
Without viewing concrete example of domain which refuses your connection - it is hard to say exactly what's going wrong.
但是,根据我的经验,我遇到了非常类似的问题,并且经常在服务器阻止包含java关键字的请求时 User-Agent
标头,默认情况下来自您的代码(不完全是java,而是包含它的字符串)。这样做是为了阻止各种机器人。
But, from my experience, I had very similar problems, and it is quite often when servers block requests which contain "java" keyword inside User-Agent
header, which comes from your code by default (not exactly "java", but string which contains it). This is done specifically in order to block various bots.
所以我建议你添加 User-Agent
标题和设置类似 Mozilla / 5.0
或简单地 Postman模拟器
,看看它是如何工作的。
So I would suggest you add User-Agent
header and set it something like Mozilla/5.0
or simply Postman emulator
and see how it will work.
这篇关于http帖子不是从代码工作,而是从邮递员工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!