Most Web servers, such as Apache, IIS, and Nginx, do not allow static files to respond to post requests

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.