Java模拟HTTP的Get和Post请求

来自:深沉的船

 

import java.io.BufferedReader;  
import java.io.BufferedWriter;  
import java.io.InputStream;  
import java.io.InputStreamReader;  
import java.io.OutputStreamWriter;  
import java.net.HttpURLConnection;  
import java.net.ProtocolException;  
import java.net.URL;  
 
import com.verisign.uuid.UUID;  
 
/**  
* 向一个飞信诈-骗网站自动提交垃圾信息的程序,用空可以运行一下。  
* @author skydong 
*  
*/  
public class AutoSubmit {  
 
  /**  
    * @param args  
    * @throws Exception     
    */  
  public static void main(String[] args) throws Exception {  
    for(int i=0; i < 100000; i++){  
      post(i);  
    }  
  }  
     
  private static void post(int i) throws Exception{  
    String s = UUID.generate().toString();  
    String s1 = s.substring(0,2);  
    s = s1+ s.substring(s.length() - 3, s.length());  
      
    URL url = new URL("http://yfs88.sv10.sgedns.cn/yy/e/qq22.asp");// 提交地址  
    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();  
    httpURLConnection.setDoOutput(true);// 打开写入属性  
    httpURLConnection.setDoInput(true);// 打开读取属性  
    httpURLConnection.setRequestMethod("POST");// 设置提交方法  
    httpURLConnection.setConnectTimeout(50000);// 连接超时时间  
    httpURLConnection.setReadTimeout(50000);  
    httpURLConnection.connect();  
      
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(httpURLConnection.getOutputStream(), "GBK"));  
    out.write("name=" + s + i +  //用户名不能重复  
        "&password=748" +  
        "&sex=日-你很行" +  
        "&oicq=748748" +  
        "&icq=748748" +  
        "&msn=caonima" +  
        "&shengri=再不关门滚蛋,就把你们全关起来" +  
        "&home=已经盯上你们了");//要post的数据,多个以&符号分割  
    out.flush();  
    out.close();  
 
      
 
    //读取post之后的返回值  
//    BufferedReader in = new BufferedReader(new InputStreamReader((InputStream) httpURLConnection.getInputStream()));  
//    String line = null;  
//    StringBuilder sb = new StringBuilder();  
//    while ((line = in.readLine()) != null) {  
//      sb.append(line);  
//    }  
//    in.close();  
//    System.out.println("client:" + sb.toString());  
      
    httpURLConnection.disconnect();//断开连接  
 
    //  
    System.out.println("client post ok:" + i);  
  }  
 
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HttpClient 是一个开源的 HTTP 客户端库,用于发送 HTTP 请求和接收响应。它提供了一系列的方法来发送 GET 和 POST 请求,并设置请求头。 以下是使用 HttpClient 发送 GET 请求的示例代码: ```java import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClientBuilder; public class HttpClientExample { public static void main(String[] args) { HttpClient httpClient = HttpClientBuilder.create().build(); try { URI uri = new URI("http://example.com/api"); HttpGet getRequest = new HttpGet(uri); // 设置请求头 getRequest.addHeader("User-Agent", "Mozilla/5.0"); HttpResponse response = httpClient.execute(getRequest); // 处理响应 System.out.println(response.getStatusLine().getStatusCode()); // 其他处理逻辑... } catch (URISyntaxException | IOException e) { e.printStackTrace(); } } } ``` 以上示例中,我们创建了一个 HttpClient 对象,并使用 `HttpGet` 请求发送 GET 请求。然后,我们通过 `addHeader` 方法设置了请求头中的 User-Agent 字段,模拟了一个浏览器的 User-Agent。最后,使用 `httpClient.execute(getRequest)` 方法发送请求并获取响应。 以下是使用 HttpClient 发送 POST 请求的示例代码: ```java import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; public class HttpClientExample { public static void main(String[] args) { HttpClient httpClient = HttpClientBuilder.create().build(); try { URI uri = new URI("http://example.com/api"); HttpPost postRequest = new HttpPost(uri); // 设置请求postRequest.addHeader("Content-Type", "application/json"); // 设置请求体 StringEntity requestBody = new StringEntity("{\"key\":\"value\"}"); postRequest.setEntity(requestBody); HttpResponse response = httpClient.execute(postRequest); // 处理响应 System.out.println(response.getStatusLine().getStatusCode()); HttpEntity responseEntity = response.getEntity(); String responseString = EntityUtils.toString(responseEntity); System.out.println(responseString); // 其他处理逻辑... } catch (URISyntaxException | IOException e) { e.printStackTrace(); } } } ``` 以上示例中,我们使用 `HttpPost` 请求发送 POST 请求。同样,我们通过 `addHeader` 方法设置了请求头中的 Content-Type 字段为 application/json。然后,我们设置了请求体为一个 JSON 字符串。最后,使用 `httpClient.execute(postRequest)` 方法发送请求并获取响应。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值