社区
Web 开发
帖子详情
关于java调用第三方接口的问题
qm13811258898
2008-10-27 06:42:05
谁做过类似于短信平台的项目,我现在做的项目中有一个模块设计到短信互动,我们已经向移动开通了第三方接口,我现在的问题是如何把我获取的参数用post方法提交到移动给我们的url上,大家做过类似项目的给我发点源码上来,我参考下,谢谢,越详细越好
...全文
1149
10
打赏
收藏
关于java调用第三方接口的问题
谁做过类似于短信平台的项目,我现在做的项目中有一个模块设计到短信互动,我们已经向移动开通了第三方接口,我现在的问题是如何把我获取的参数用post方法提交到移动给我们的url上,大家做过类似项目的给我发点源码上来,我参考下,谢谢,越详细越好
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
10 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
地铁18号线
2011-07-20
打赏
举报
回复
qm13811258898
2008-10-28
打赏
举报
回复
那就是我的用户名和密码没有通过,或者是那边根本就没有获取到了???
qm13811258898
2008-10-28
打赏
举报
回复
搞定,就此结贴,谢谢上面2位给的网址,我看了下,有些帮助,不过最后原因是没写HTTP认证,多谢了!
sunyujia
2008-10-27
打赏
举报
回复
401 Unauthorized 客户试图未经授权访问受密码保护的页面。应答中会包含一个WWW-Authenticate头,浏览器据此显示用户名字/密码对话框,然后在填写合适的Authorization头后再次发出请求。
原文网址:http://blog.csdn.net/sunyujia/archive/2008/05/02/2362800.aspx
楼主应该和对方沟通下.
bruni
2008-10-27
打赏
举报
回复
你的代码这么乱, 让别人怎么看? CSDN不是有"插入源代码"就是那个带#号的图片按钮的么?
qm13811258898
2008-10-27
打赏
举报
回复
这个我写的类,我在测试的时候总是出现401错误
package com.crayze.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import org.apache.log4j.Logger;
public class HttpClient{
private static Logger log = Logger.getLogger(HttpClient.class);
/**
* 功能: 向呼叫控制发送呼叫信息
* @param encode
* @param sparametersPOST 的参数
* @param curl呼叫控制的链接
* @param smethodhttp请求方法 GET OR POST
* @return*/
public static String sendPostRequest(String encode,String sparameters, String curl){
String strRtn = "-1";
URL url = null;
String str = null;
OutputStreamWriter os = null;
BufferedReader is = null;
HttpURLConnection httpConn = null;
try{
url = new URL(curl);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.setRequestMethod("POST");
os = new OutputStreamWriter(httpConn.getOutputStream(), encode);
os.write(sparameters);
os.flush();
os.close();
is = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
while ((str = is.readLine()) != null){
strRtn = str;
}
is.close();
os = null;
is = null;
str = null;
httpConn.disconnect();
}
catch (MalformedURLException e){
strRtn = "malError";
e.printStackTrace();
}
catch (UnknownHostException unhostex){
log.debug("in requestUrl() UnknownHostException error: "+ unhostex.getMessage());
strRtn = "unknowError";
}
catch (IOException ex){
log.debug("in requestUrl() error: " + ex.toString());
strRtn = "ioError";
}
finally{
try{
if (os != null)
os.close();
if (is != null)
is.close();
}
catch (IOException e){
e.printStackTrace();
}
}
log.debug("strRtn is [" + strRtn + "]");
return strRtn;
}
/**
* @param encode
* @param sparameters
* @param curl
* @return
*/
public static String sendGetRequest(String encode,String sparameters, String curl){
String strRtn = "-1";
URL url = null;
String str = null;
BufferedReader is = null;
HttpURLConnection httpConn = null;
StringBuffer sb = null;
try{
sb = new StringBuffer(curl);
sb.append("?");
sb.append(sparameters);
url = new URL(sb.toString());
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.setRequestMethod("GET");
is = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
while ((str = is.readLine()) != null){
strRtn = str;
}
is.close();
is = null;
str = null;
httpConn.disconnect();
}
catch (MalformedURLException e){
strRtn = "malError";
e.printStackTrace();
}
catch (UnknownHostException unhostex){
log.debug("in requestUrl() UnknownHostException error: "+ unhostex.getMessage());
strRtn = "unknowError";
}
catch (IOException ex){
log.debug("in requestUrl() error: " + ex.toString());
strRtn = "ioError";
}
finally{
try
{
if (is != null)
is.close();
}
catch (IOException e){
e.printStackTrace();
}
}
log.debug("strRtn is [" + strRtn + "]");
return strRtn;
}
}
上面的是HttpClient类,下面的类是获取的一些数据,并把这些数据调用这个HttpClient类传出去,、
package com.crayze.phonebox.sms;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import com.crayze.util.HttpClient;
import com.crayze.util.ManipulateXml;
public class Sms9588AdapterServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
private Logger log = Logger.getLogger(Sms9588AdapterServlet.class);
private String strLogicUrl = "";
// 成功发送
private static int SEND_OK = 0;
// 目前短信业务平台定义的错误代码都是>0的整数在这里防止和平台定义冲突可以用负整数定义
// 连接错误
private static int CONNECT_ERROR = -1000;
// IO异常
private static int IO_ERROR = -2000;
// 分析短信业务平台回应的错误
private static int WAPDM_ERROR = -3000;
// Initialize global variables
public void init(ServletConfig config) throws ServletException {
strLogicUrl = config.getInitParameter("logicurl");
}
// Process the HTTP Get request
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
String strTemp = null;
String strRtn = null;
try {
Document doc = null;
doc = ManipulateXml.readXmlDocumentFromStream(request.getInputStream());
Element root = doc.getDocumentElement();
String Message = ManipulateXml.getElementValue(ManipulateXml.firstElementByName(root, "MESSAGE"));
String clientPhone = ManipulateXml.getElementValue(ManipulateXml.firstElementByName(root, "MSISDN"));
log.debug("before transfor Message Content is [ " + Message + "]");
// log.debug("Message Content is [ " + strTemp + "]");
log.debug("clientPhone is [ " + clientPhone + "]");
SmbppSendUnicodeMessage("", "4083MFBZ4083M0000", "", clientPhone,clientPhone, Message, 0, 0, 0, 0, 0, 0);
StringBuffer sb = new StringBuffer("");
// sb.append(strReqInitParam);
sb.append("clientPhone=");
sb.append(clientPhone);
sb.append("&subsPhone=");
// sb.append("0106432975-1049,13811258898");
sb.append("13811258898");
sb.append("&timeLen=3600");
// sb.append(strTimeLen);
sb.append("&ivrflowtype=121");
// sb.append(ivrflowtype);
log.debug("param is " + sb.toString());
log.debug("request url " + strLogicUrl);
// 发送呼叫请求
strRtn = HttpClient.sendPostRequest("GBK", sb.toString(),strLogicUrl);
log.debug("HttpClient rtn is: " + strRtn);
} catch (SAXException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
this.doPost(request, response);
}
private void SmbppSendUnicodeMessage(String bstrMoMessageID,String bstrBusinessCode, String bstrLongCode,
String bstrFeeMsisdn,String bstrDesMsisdn, String bstrMessageContent, int lTpPid,
int lTpUdhi, int lSendDate, int lSendTime, int lExpireDate,int lExpireTime) {
// POST的参数
String paramStr = "bstrMoMessageID=" + bstrMoMessageID
+ "&bstrBusinessCode=" + bstrBusinessCode + "&bstrLongCode="
+ bstrLongCode + "&bstrFeeMsisdn=" + bstrFeeMsisdn
+ "&bstrDesMsisdn=" + bstrDesMsisdn + "&bstrMessageContent="
+ bstrMessageContent + "<pPid=" + lTpPid + "<pUdhi="
+ lTpUdhi + "&lSendDate=" + lSendDate + "&lSendTime="
+ lSendTime + "&lExpireDate=" + lExpireDate + "&lExpireTime="
+ lExpireTime + "&username=4584&password=nvuhejidjk";
// 发送UnicodeMess的URL
String destURL = "http://59.151.7.69/SmbpHttpAgent/SmbpHttpAgent.asmx/SmbppSendUnicodeMessage";
// 调用向短信业务平台提交数据的方法
String rtn = null;
rtn = HttpClient.sendPostRequest("GBK", paramStr, destURL);
log.debug("send rtn is " + rtn);
public void destroy() {
}
}
帮我看看有什么问题,谢谢
sunyujia
2008-10-27
打赏
举报
回复
我一直用apache的,下面的文章很详细而且也很简明/
http://www.cnblogs.com/kentyshang/archive/2008/05/22/1204865.html
bruni
2008-10-27
打赏
举报
回复
apache有个httpclient项目, 里面就有POST的方法:
http://hc.apache.org/httpclient-3.x/methods/post.html
FineReport报表API源代码(报表
调用
)
本代码主要开源了
java
报表工具中报表
调用
部分的代码,通过该开源代码,你可以了解报表工具的内部原理,掌握国内主流
java
报表工具的各类
接口
使用方法,并运用该
接口
对软件产品或项目中的个性化的需求进行二次开发。...
JAVA
_API1.6文档(中文)
java
x.sql.rowset.spi
第三方
供应商在其同步提供者的实现中必须使用的标准类和
接口
。
java
x.swing 提供一组“轻量级”(全部是
Java
语言)组件,尽量让这些组件在所有平台上的工作方式都相同。
java
x.swing....
微信公众平台企业号开发
Java
版第6课——JS-SDK
课程以微信公众号开发视角,讲解
JAVA
开发微信公众号所需的框架、
第三方
工具。 购买套餐还赠送经典微信开发课程——[微信公众号_独立知识点]环境搭建。该课程针对各种复杂的网络环境,讲解如何构建开发环境,已解决...
Java
调用
第三方
接口
示范
在项目开发中经常会遇到
调用
第三方
接口
的情况,比如说
调用
第三方
的天气预报
接口
。 使用流程 【1】准备工作:在项目的工具包下导入HttpClientUtil这个工具类,或者也可以使用Spring框架的restTemplate来
调用
,上面...
Java
调用
第三方
接口
方法
Java
调用
第三方
接口
方法 一、 通过JDK网络类
Java
.net.HttpURLConnection 1.
java
.net包下的原生
java
api提供的http请求 使用步骤: 1、通过统一资源定位器(
java
.net.URL)获取连接器(
java
.net.URLConnection)。 ...
Web 开发
81,091
社区成员
341,718
社区内容
发帖
与我相关
我的任务
Web 开发
Java Web 开发
复制链接
扫一扫
分享
社区描述
Java Web 开发
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章