怎么用java 发送http报文
写出关键的类和方法就行不知道是不是这个?HttpClienthttpClient=newHttpClient();response.setCharacterEncodin...
写出关键的类和方法就行
不知道是不是这个?
HttpClient httpClient = new HttpClient();
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
//设置请求地址
PostMethod postMethod = new PostMethod("https://www.umbpay.com/pay2_1_/merAccountCheck.do");
//PostMethod postMethod = new PostMethod("http://localhost:8081/umpay/merAccountCheck.do");
// 设置用户浏览器为MicrosoftIE6.0
postMethod.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
//设置请求的编码方式
//postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF-8");
//设置请求参数
postMethod.addParameter("merchantId",checkForm.getMerchantId());
postMethod.addParameter("accountId",checkForm.getAccountId());
postMethod.addParameter("currencyType",checkForm.getCurrencyType());
postMethod.addParameter("payBank",checkForm.getPayBank());
postMethod.addParameter("startTime",checkForm.getStartTime());
postMethod.addParameter("endTime",checkForm.getEndTime());
postMethod.addParameter("mac",getSendMac(checkForm));
// 获取执行结果
int status = httpClient.executeMethod(postMethod);
//但是这个是用参数在传,发送报文应该不用参数名吧?
该怎么做?
用http协议来传,不用tcp ip传
是这个:postMethod.setRequestBody(sendStr); 展开
不知道是不是这个?
HttpClient httpClient = new HttpClient();
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
//设置请求地址
PostMethod postMethod = new PostMethod("https://www.umbpay.com/pay2_1_/merAccountCheck.do");
//PostMethod postMethod = new PostMethod("http://localhost:8081/umpay/merAccountCheck.do");
// 设置用户浏览器为MicrosoftIE6.0
postMethod.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
//设置请求的编码方式
//postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF-8");
//设置请求参数
postMethod.addParameter("merchantId",checkForm.getMerchantId());
postMethod.addParameter("accountId",checkForm.getAccountId());
postMethod.addParameter("currencyType",checkForm.getCurrencyType());
postMethod.addParameter("payBank",checkForm.getPayBank());
postMethod.addParameter("startTime",checkForm.getStartTime());
postMethod.addParameter("endTime",checkForm.getEndTime());
postMethod.addParameter("mac",getSendMac(checkForm));
// 获取执行结果
int status = httpClient.executeMethod(postMethod);
//但是这个是用参数在传,发送报文应该不用参数名吧?
该怎么做?
用http协议来传,不用tcp ip传
是这个:postMethod.setRequestBody(sendStr); 展开
3个回答
展开全部
URL url = null;
HttpURLConnection httpurlconnection = null;
try {
url = new URL("http://211.147.222.30/se/sseS");
// 以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=user&Password=pwd&SrcNumber=1065&DestTermID="
+ 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();
}
HttpURLConnection httpurlconnection = null;
try {
url = new URL("http://211.147.222.30/se/sseS");
// 以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=user&Password=pwd&SrcNumber=1065&DestTermID="
+ 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();
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用流啊,获取到输出流,然后用流输出报文内容就ok了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
网页链接这玩意网上多的事,学编程不知道怎么搜索自己想要的东西?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询