Recently called an interface, found that HttpPost request the target site will appear 405 status code, because Apache, IIS, Nginx and so on most of the Web server, do not allow static files to respond to the POST request
So change the POST request to a GET request
Package Com.changyou.test;import Java.io.ioexception;import Java.io.unsupportedencodingexception;//import Org.apache.commons.httpclient.HttpClientimport Org.apache.commons.httpclient.httpclient;import Org.apache.commons.httpclient.httpexception;import org.apache.commons.httpclient.UsernamePasswordCredentials; Import Org.apache.commons.httpclient.auth.authscope;import Org.apache.commons.httpclient.methods.GetMethod; Import Org.apache.commons.httpclient.methods.postmethod;import Org.apache.commons.httpclient.methods.stringrequestentity;import Org.apache.commons.logging.logfactory;import Org.apache.commons.codec.decoderexception;public class Animaltest {public static void main (string[] args) throws IOException {System.out.println ("POST request starts ..."),//httpwebrequest request =httpclient client = new HttpClient ();// String url = "http://10.128.36.39:8088/180.96.47.133"; String url = "Http://10.128.36.39:8088/servers.txt"; Postmethod postmethod= new Postmethod (URL); GetMethod GetMethod = new GetMethod (URL); Usernamepasswordcredentials creds = new Usernamepasswordcredentials ("Anchen", "* * *"); The password is not visible getmethod.addrequestheader ("Content-type", "Application/x-www-form-urlencoded;charset = Utf-8"); Client.getparams (). Setauthenticationpreemptive (True); Client.getstate (). SetCredentials (Authscope.any, creds);p ostmethod.setdoauthentication (true);//String payload= "{{ \ "gametype\": \ "1\", \ "cmd\": \ "102\", \ "worldid\": \ "154\", \ "logtype\": \ "item\", \ "date\": \ "2017-08-17\", \ " Keywordone\ ": \" \ ", \" keywordtwo\ ": \" \ "}}"; String payload = ""; try {postmethod.setrequestentity (new stringrequestentity (payload, "text/html", "utf-8");// Getmethod.set} catch (Unsupportedencodingexception E1) {e1.printstacktrace ();} try {//client.executemethod (Postmethod); Client.executemethod (GetMethod);} catch (HttpException E1) { E1.printstacktrace ();} catch (IOException E1) {e1.printstacktrace ();} System.out.println (Postmethod.getstatuscode ());//system.out.println (postmethod.getresponsebodyasstring ()); System.out.println (GETMETHOD.GEtstatuscode ());//system.out.println (getmethod.getresponsebodyasstring ()); System.out.println (New String (Getmethod.getresponsebody (), "UTF-8"));/*postmethod.addrequestheader ("Content-type "," Application/x-www-form-urlencoded;charset = Utf-8 ");p ostmethod.setrequestbody (" "); try {Client.executemethod ( Postmethod);} catch (HttpException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} String result = ""; try {result = new String (Postmethod.getresponsebody (), "Utf-8");} catch (Unsupportedencodingexception e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo auto-generated catch Blocke.pri Ntstacktrace ();} SYSTEM.OUT.PRINTLN ("Result:" +result); */postmethod.releaseconnection (); Getmethod.releaseconnection ();}}
Most Web servers, such as Apache, IIS, and Nginx, do not allow static files to respond to post requests