您的位置:

关于java发送http指令的信息

本文目录一览:

怎么用java 发送http报文

URL url = null;

HttpURLConnection httpurlconnection = null;

try {

url = new URL("");

// 以post方式请求

httpurlconnection = (HttpURLConnection) url.openConnection();

httpurlconnection.setConnectTimeout(6000);

httpurlconnection.setReadTimeout(6000);

httpurlconnection.setDoOutput(true);

httpurlconnection.setRequestMethod("POST");

msg=java.net.URLEncoder.encode(msg,"utf-8");

String username = "UserName=userPassword=pwdSrcNumber=1065DestTermID="

+ dest+ "MsgContent=" + msg;

httpurlconnection.getOutputStream().write(

username.getBytes("utf-8"));

httpurlconnection.getOutputStream().flush();

httpurlconnection.getOutputStream().close();

// 获取响应代码

code = httpurlconnection.getResponseCode();

// 获取页面内容

java.io.InputStream in = httpurlconnection.getInputStream();

java.io.BufferedReader breader = new BufferedReader(

new InputStreamReader(in, "gb2312"));

String str = breader.readLine();

while (str != null) {

resp+=str;

str= breader.readLine();

}

} catch (Exception e) {

resp="err";

} finally {

if (httpurlconnection != null)

httpurlconnection.disconnect();

}

java怎么主动发送http请求

实现思路就是先定义请求头内容,之后进行请求头设置。

定义请求头

LinkedHashMapString,String headers = new LinkedHashMapString,String();

headers.put("Content-type","text/xml");

headers.put("Cache-Control", "no-cache");

headers.put("Connection", "close");

给HttpPost 设置请求头

HttpPost httpPost = new HttpPost("地址");

if (headers != null) {

for (String key : headers.keySet()) {

httpPost.setHeader(key, headers.get(key));

}

}

备注:只需要在map中设置相应的请求头内容即可。根据实际需要修改即可

如何使用java http发送请求

HttpURLConnection mHttpURLConnection = null; URL mUrl = null; InputStream inputStream = null; try { String url = UrlEncode(mFileType.getStrUrl(), "UTF-8"); //mUrl = new URL(mFileType.getStrUrl()); mUrl = new URL(url); mHttpURLConnection = (HttpURLConnection) mUrl.openConnection(); mHttpURLConnection.setAllowUserInteraction(true); mHttpURLConnection.setRequestMethod("GET"); mHttpURLConnection.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); mHttpURLConnection.setRequestProperty("Connection", "Keep-Alive"); int responseCode = mHttpURLConnection.getResponseCode(); long length = mHttpURLConnection.getContentLength(); // 判断请求是否成功处理 if (responseCode == HttpStatus.SC_OK||responseCode == HttpStatus.SC_PARTIAL_CONTENT) { inputStream = mHttpURLConnection.getInputStream(); }catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }

java 怎么发送一个http请求

这里有整套的Java发送HTTP请求的代码,包括Post、Get、Delete、Put、Trace、Head、Options请求方法,链接如下:

然后线上实力Demo: