HttpURLConnection httpURLConnection;
  try{
  URL url2 = new URL(url);
  httpURLConnection = (HttpURLConnection)url2.openConnection(); // 获取连接
  httpURLConnection.setRequestMethod("POST"); // 设置请求方法为POST, 也可以为GET
  httpURLConnection.setDoOutput(true);
  httpURLConnection.setDoInput(true);
  httpURLConnection.setUseCaches(false);
        // 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成,
        // 要注意的是connection.getOutputStream会隐含的进行connect。
        httpURLConnection.connect();
        String inf="?json="+content;
  OutputStream os = httpURLConnection.getOutputStream();
  os.write(inf.getBytes());
  os.flush();
  os.close();
我用上面的代码向服务器发送数据,其中:发送地址是url,数据时content,json格式的,不知为什么发送不过去。
我知道是不是因为content的字符长度过长,大概它的长度有300

解决方案 »

  1.   

    先测试下连接是否成功,一步步来,可以把你的URL贴出来给我们看看
      

  2.   

    package com.xiaoxu.ws;import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.net.URLConnection;public class SOAP_httpClient { //SOAP 客户端调用 (http Post) 2012-08-02
    public static void main(String[] args) throws IOException {
    String SOAPUrl      = "http://localhost:8081/illuma/illuma";
    String xmlSend="sssd";
     String SOAPAction = "testHello";

     URL url = new URL(SOAPUrl);
     URLConnection connection = url.openConnection();
     HttpURLConnection httpConn = (HttpURLConnection) connection;
     //FileInputStream fin = new FileInputStream(xmlFile2Send);
     //ByteArrayOutputStream bout = new ByteArrayOutputStream();    
     // Copy the SOAP file to the open connection.
     //copy(fin,bout);
     //fin.close();
     byte[] b = xmlSend.getBytes();
     // Set the appropriate HTTP parameters.
     httpConn.setRequestProperty( "Content-Length",String.valueOf( b.length ) );
     httpConn.setRequestProperty("Content-Type","text/xml; charset=utf-8");
     httpConn.setRequestProperty("SOAPAction",SOAPAction);
     httpConn.setRequestMethod( "POST" );
     httpConn.setDoOutput(true);
     httpConn.setDoInput(true);
     // Everything's set up; send the XML that was read in to b.
     OutputStream out = httpConn.getOutputStream();
     out.write( b );    
     out.close();
     
     InputStreamReader isr = new InputStreamReader(httpConn.getInputStream());
     BufferedReader in = new BufferedReader(isr);
     String inputLine;
     while ((inputLine = in.readLine()) != null)
     System.out.println(inputLine);
     in.close();
     
     
    }
    }你好这是我写的一个简单调用例子!看看能不能帮你。