用socket发送HTTP请求时出现这样的错误,求教!

jay314159 2009-12-19 11:48:45
学校内有一个网站提供了SOAP服务,
虽然通过web引用可以很方便地访问并取得返回信息,
但我想通过自己发送socket过去。
但是总是提示有错。
麻烦大侠们帮我看看。
我构造的socket如下:

package sockettest;

import java.net.*;
import java.io.*;
/**
*
* @author 雁过留声
*/
public class Connect {
static Socket socket;

public static boolean Connect(String ip, int port) throws Exception
{
try {
socket = new Socket(ip, port);
PrintStream out;
BufferedReader in;

out = new PrintStream(socket.getOutputStream());
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

StringBuffer sb = new StringBuffer("POST /netfilter/UserLogon.asmx HTTP/1.1\r\n");
sb.append("Host: auth.stu.edu.cn\r\n");
sb.append("Content-Type: text/xml; charset=utf-8\r\n");
sb.append("Content-Length: 466\r\n");
sb.append("SOAPAction: \"http://tempuri.org/ClientLogon\"\r\n\r\n");

sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sb.append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
sb.append("<soap:Body>");
sb.append("<ClientLogon xmlns=\"http://tempuri.org/\">");
sb.append("<userName>07lslin1</userName>");
sb.append("<password>123</password>");
sb.append("<userIp>10.14.2.146</userIp>");
sb.append("<hostName>null</hostName>");
sb.append("<userMac>null</userMac>");
sb.append("<userAgent>null</userAgent>");
sb.append("<isVisiteForeign>true</isVisiteForeign>");
sb.append("</ClientLogon>");
sb.append("</soap:Body>\r\n");
sb.append("</soap:Envelope>\r\n");

OutputStream socketOut = socket.getOutputStream();
socketOut.write(sb.toString().getBytes());

InputStream socketIn = socket.getInputStream();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
byte[] buff = new byte[1024];
int len = -1;
while((len=socketIn.read(buff))!=-1)
{
buffer.write(buff, 0, len);
}
System.out.println(new String (buffer.toByteArray()));
return true;
} catch (IOException ex) {//连接不成功
System.out.println("异常:"+ex.toString());
}
return false;
}

public static void main(String[] rgs) throws Exception
{
boolean result = Connect("auth.stu.edu.cn",80);
System.out.println(result);
}
}



但是发送出去时,打印出这样的信息:

==============================================================================
HTTP/1.1 500 内部服务器错误。
Date: Sat, 19 Dec 2009 03:36:43 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 1.1.4322
Cache-Control: private
Content-Type: text/xml; charset=utf-8
Content-Length: 579

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<soap:Fault>
<faultcode>soap:Client</faultcode>
<faultstring>服务器无法读取请求。 --> XML 文档(1, 439)中有错误。 --> 行“1”上的开始标记“ClientLogon”与结束标记“ClientLogo”不匹配。 行 1,位置 457。</faultstring>
<detail />
</soap:Fault>
</soap:Body>
</soap:Envelope>HTTP/1.1 400 Bad Request
Content-Type: text/html
Date: Sat, 19 Dec 2009 03:36:43 GMT
Connection: close
Content-Length: 35

<h1>Bad Request (Invalid Verb)</h1>
true
成功生成(总时间:0 秒)

==============================================================================


SOAP服务端要求发送请求的格式如下:

******************************************************************************
ClientLogon

测试
测试窗体只能用于来自本地计算机的请求。
SOAP
下面是一个 SOAP 请求和响应示例。所显示的占位符需要由实际值替换。
//////////////////////////////////////请求示例

POST /netfilter/UserLogon.asmx HTTP/1.1
Host: auth.stu.edu.cn
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/ClientLogon"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ClientLogon xmlns="http://tempuri.org/">
<userName>string</userName>
<password>string</password>
<userIp>string</userIp>
<hostName>string</hostName>
<userMac>string</userMac>
<userAgent>string</userAgent>
<isVisiteForeign>boolean</isVisiteForeign>
</ClientLogon>
</soap:Body>
</soap:Envelope>

//////////////////////////////////////响应示例

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ClientLogonResponse xmlns="http://tempuri.org/">
<ClientLogonResult>string</ClientLogonResult>
</ClientLogonResponse>
</soap:Body>
</soap:Envelope>
******************************************************************************

...全文
434 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunnylyy 2009-12-19
  • 打赏
  • 举报
回复
sb.append("</soap:Body>\r\n");
sb.append("</soap:Envelope>\r\n");

去掉\r\n试试看,如果还不行就自己安装个抓包工具,看看自己到底给服务端发了什么消息
jay314159 2009-12-19
  • 打赏
  • 举报
回复
有道理,不过,我改为501之后,出现了另一个错误:

HTTP/1.1 500 内部服务器错误。
Date: Sat, 19 Dec 2009 04:22:56 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 1.1.4322
Cache-Control: private
Content-Type: text/xml; charset=utf-8
Content-Length: 506

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<soap:Fault>
<faultcode>soap:Client</faultcode>
<faultstring>服务器无法读取请求。 --> 这是一个意外的标记。标记应为“TAGEND”。 行 4,位置 16。</faultstring>
<detail />
</soap:Fault>
</soap:Body>
</soap:Envelope>HTTP/1.1 400 Bad Request
Content-Type: text/html
Date: Sat, 19 Dec 2009 04:22:56 GMT
Connection: close
Content-Length: 35

<h1>Bad Request (Invalid Verb)</h1>
true
sunnylyy 2009-12-19
  • 打赏
  • 举报
回复
“ClientLogon”与结束标记“ClientLogo”不匹配

再看这句:
sb.append("Content-Length: 466\r\n");

肯定是长度不够,466, 刚好到"ClientLogo",后面的没有传。这个长度要先拼出soap字符串,然后传字符串的长度.
crazylaa 2009-12-19
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jay314159 的回复:]
有道理,不过,我改为501之后,出现了另一个错误:

HTTP/1.1 500 内部服务器错误。
Date: Sat, 19 Dec 2009 04:22:56 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 1.1.4322
Cache-Control: private
Content-Type: text/xml; charset=utf-8
Content-Length: 506

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <soap:Fault>
      <faultcode>soap:Client </faultcode>
      <faultstring>服务器无法读取请求。 --> 这是一个意外的标记。标记应为“TAGEND”。 行 4,位置 16。 </faultstring>
      <detail />
    </soap:Fault>
  </soap:Body>
</soap:Envelope>HTTP/1.1 400 Bad Request
Content-Type: text/html
Date: Sat, 19 Dec 2009 04:22:56 GMT
Connection: close
Content-Length: 35

<h1>Bad Request (Invalid Verb) </h1>
true

[/Quote]

\r\n的问题。

62,614

社区成员

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

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