关于java调用第三方接口的问题

qm13811258898 2008-10-27 06:42:05
谁做过类似于短信平台的项目,我现在做的项目中有一个模块设计到短信互动,我们已经向移动开通了第三方接口,我现在的问题是如何把我获取的参数用post方法提交到移动给我们的url上,大家做过类似项目的给我发点源码上来,我参考下,谢谢,越详细越好
...全文
1149 10 打赏 收藏 转发到动态 举报
写回复
用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

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧