将xml数据发送到其他域(跨域)使用java

时间:2013-10-09 10:02:49

标签: java httpclient

我正在尝试将xml格式的数据发送到其他服务器,这意味着使用像HttpClient这样的java跨域。我已经检查了stackoverflow和mkyong中的其他帖子,但没有任何对我有用。在这篇文章中

[http://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/] [1]正在运行,但我不知道如何在不使用参数的情况下发送数据

请指导前进。 谢谢你的帮助。

修改

我正在使用此代码,它适用于我。

 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.util.ArrayList;
 import java.util.List;

 import org.apache.http.HttpResponse;
 import org.apache.http.NameValuePair;
 import org.apache.http.client.ClientProtocolException;
 import org.apache.http.client.entity.UrlEncodedFormEntity;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.http.message.BasicNameValuePair;

 public class workingCode {
public static void main(String[] args) throws ClientProtocolException, IOException       {


  try {
      DefaultHttpClient client = new DefaultHttpClient();

    String url = "https://selfsolve.apple.com/wcResults.do";

    HttpPost post = new HttpPost(url);
    // add header
    post.setHeader("User-Agent", "Mozilla/5.0");

        List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
        urlParameters.add(new BasicNameValuePair("sn", "C02G8416DRJM"));
        urlParameters.add(new BasicNameValuePair("cn", ""));
        urlParameters.add(new BasicNameValuePair("locale", ""));
        urlParameters.add(new BasicNameValuePair("caller", ""));
        urlParameters.add(new BasicNameValuePair("num", "12345"));

        post.setEntity(new UrlEncodedFormEntity(urlParameters));

        HttpResponse response = client.execute(post);
        System.out.println("Response Code : "     + response.getStatusLine().getStatusCode()+"    Response StatusLine : "     + response.getStatusLine());

        InputStreamReader isp = new InputStreamReader(response.getEntity().getContent());
        BufferedReader rd = new BufferedReader(isp);

        StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }
} catch (IllegalStateException e ) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}catch (ClientProtocolException cpe) {
        // TODO Auto-generated catch block
        cpe.printStackTrace();
}catch (IOException ie) {
        // TODO Auto-generated catch block
        ie.printStackTrace();
} 
}
}

This is working but i need to send the data like this.

StringEntity entity = new StringEntity(data, HTTP.UTF_8);
entity.setContentType("application/x-www-form-urlencoded");
post.setEntity(entity);

当我通过将url更改为客户端url来执行此操作时,它失败但是当使用javascript执行此操作时,它会失败,说“HTTP / 1.1 401 Unauthorized”。

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

HttpClient client = HttpClientBuilder.create().build();
      HttpResponse response = null;
      try {    

            String url = ResourceBundle.getBundle("resourses").getString("url");

            HttpPost post = new HttpPost(url);
            // add header
            post.setHeader("User-Agent", "Mozilla/5.0");

            StringEntity entity = new StringEntity(StringXMLDATA, HTTP.UTF_8);

            entity.setContentType("application/x-www-form-urlencoded");
            entity.setChunked(true);
            post.setEntity(entity);

            response = client.execute(post);
            System.out.println("Response Code : "     + response.getStatusLine().getStatusCode()+"    Response StatusLine : "     + response.getStatusLine());

            InputStreamReader isp = new InputStreamReader(response.getEntity().getContent());
            BufferedReader rd = new BufferedReader(isp);

            StringBuffer result = new StringBuffer();
            String line = "";
            while ((line = rd.readLine()) != null) {
                System.out.println("line : "+line);
                result.append(line);
            }
            System.out.println("Result :   " + result);
    }catch (Exception cpe) {
        System.out.println(cpe);
    }finally { 
        client.getConnectionManager().shutdown(); 
    }

对于此代码,您需要添加3 Jar我在下面给出的名称,您可以从此链接下载这些罐子http://hc.apache.org/downloads.cgi

JARs NEEDS

共享记录-1.1.jar
HTTP客户端-4.3.1.jar
HTTP的芯 - 4.3.jar