Java post 服务器,java post数据服务器拒绝的解决方法

当我们采用post数据的时候有可能会遇到这种情况服务器返回403错误。这里只要我们设置一下UA欺骗一下服务器就可以了。有时候设置了windowNT的时候没有办法解决记得设置一下linux下面的。这里给出我自己封装的HTTP 的类采用的是httpclient 这个包。

public class Http {

private static Http mhttp;

private HttpClient client = null;

private Http() {

client = new DefaultHttpClient();

}

public static Http getInstance() {

if (mhttp == null) {

mhttp = new Http();

}

return mhttp;

}

public String doPost(String url, List params) {

return doPost(url, params, "UTF-8");

}

public synchronized String doPost(String url, List params, String code) {

//HttpEntity entity = new Defaulthttp

String result = null;

HttpPost post = new HttpPost(url);

HttpEntity entity = null;

try {

entity = new UrlEncodedFormEntity(params, code);

//client.getParams().setParameter(CoreConnectionPNames., arg1)

//post.addHeader("content-type", "application/x-www-form-urlencoded");

//重点就在这里

post.addHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.160 Safari/537.22");

post.addHeader("Accept-Encoding", "gzip");

post.addHeader("Accept-Charset", "utf-8");

post.addHeader("Accept-Language", "en-US,en");

//post.addHeader("accept", "*/*");

post.addHeader("connection", "Keep-Alive");

post.setEntity(entity);

HttpResponse httpResponse = client.execute(post);

if (httpResponse != null && httpResponse.getStatusLine().getStatusCode() == 200) {

result = EntityUtils.toString(httpResponse.getEntity());

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

}

return result;

}

public synchronized String doGet(String url) {

return doGet(url, "utf-8");

}

public String doGet(String url, String code) {

String result = null;

HttpGet get = new HttpGet(url);

try {

HttpResponse httpResponse = client.execute(get);

if (httpResponse != null && httpResponse.getStatusLine().getStatusCode() == 200) {

result = EntityUtils.toString(httpResponse.getEntity());

}

} catch (ClientProtocolException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

}

return result;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值