Web服务器Apache接收响应,Apache、IIS、Nginx等绝大多数web服务器,都不允许静态文件响应POST请求(示例代码)...

该代码示例展示了如何使用Apache HttpClient库进行POST和GET HTTP请求。它创建了HttpClient实例,设置了认证信息,并对指定URL执行请求。请求包括设置Content-Type头、处理请求体以及释放连接等步骤。
摘要由CSDN通过智能技术生成

package com.changyou.test;

import java.io.IOException;

import java.io.UnsupportedEncodingException;

//import org.apache.commons.httpclient.HttpClient

import 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 block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println("result:"+result);*/

postMethod.releaseConnection();

getMethod.releaseConnection();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值