Apache、IIS、Nginx等絕大多數web伺服器,都不允許靜態檔案響應POST請求

來源:互聯網
上載者:User

標籤:開始   key   pcl   gty   靜態   urlencode   int   pack   rate   

最近調用一個介面,發現httppost請求目標網站會出現405 狀態代碼,原因為 Apache、IIS、Nginx等絕大多數web伺服器,都不允許靜態檔案響應POST請求

所以將post請求改為get請求即可

 
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請求開始...");//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", "****"); //密碼不可見getMethod.addRequestHeader("Content-Type"," application/x-www-form-urlencoded;charset = utf-8");client.getParams().setAuthenticationPreemptive(true); client.getState().setCredentials(AuthScope.ANY, creds);postMethod.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");postMethod.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.printStackTrace();}System.out.println("result:"+result);*/postMethod.releaseConnection();getMethod.releaseConnection();}}

  

Apache、IIS、Nginx等絕大多數web伺服器,都不允許靜態檔案響應POST請求

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.