请教一个问题

hdqqq 2012-06-13 09:09:48
用java准备请求一个url连接,url带查询参数。实现函数如下:

public static void open_url()
{
try {
String up_url = "http://192.168.1.60/upload/Default.aspx?id=1234567890";
java.net.URL url = new java.net.URL(up_url);
java.net.HttpURLConnection conn = (java.net.HttpURLConnection)url.openConnection();
}
catch (java.net.MalformedURLException e) {
e.printStackTrace();
}
catch (java.io.IOException e) {
e.printStackTrace();
}
}
服务端用.net 实现,现在问题是服务端取不到id和提交的参数。
想问的问题是,java这样增加参数是否正确,还是需要其他方法。
...全文
140 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
MiceRice 2012-06-13
  • 打赏
  • 举报
回复
这样增加参数是可以的,不过有两个问题:
1、漏了开启动作;
2、.Net端,用的是POST方式接收参数还是GET方式?

String urlString = "http://192.168.1.60/upload/Default.aspx?id=1234567890";
URL url = new URL(urlString);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection()
httpConn.setRequestMethod("GET"); // 访问方式
httpConn.connect(); // 关键是这个动作别漏了
岁月之梦 2012-06-13
  • 打赏
  • 举报
回复
httpclient 例子很多哦 到网上看看
a395885670 2012-06-13
  • 打赏
  • 举报
回复

InputStream inputStream = null;
BufferedReader in = null;
String path = null;
try {
path = ROOT_PATH
+ URLEncoder.encode(MESSAGE + smsContent + PHONE
+ phoneNumber + key, "utf-8");
URL url;
url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(15000);
conn.setReadTimeout(15000);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-type", "text/html");
conn.setRequestProperty("Accept-Charset", "utf-8");
conn.setRequestProperty("contentType", "utf-8");
conn.setDoInput(true);
inputStream = conn.getInputStream();
String line;
in = new BufferedReader(new InputStreamReader(inputStream), 1024);
while ((line = in.readLine()) != null) {
if (line.length() > 0) {
Gson gson = new Gson();
Type type = new TypeToken<SMSResult>() {
}.getType();
smsResult = gson.fromJson(line, type);
}
}
} catch (Exception e) {
throw e;
} finally {
try {
if (null != in) {
in.close();
}
if (null != inputStream) {
inputStream.close();
}
} catch (Exception e) {
throw e;
}
}



楼主可以参考下 需要用POST

62,614

社区成员

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

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