Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java将HTTP请求GET/POST发送到表单?_Java_Http_Http Post_Http Get - Fatal编程技术网

使用Java将HTTP请求GET/POST发送到表单?

使用Java将HTTP请求GET/POST发送到表单?,java,http,http-post,http-get,Java,Http,Http Post,Http Get,所以我有了这段代码,我让它开始工作,现在它基本上允许我向几乎任何我想要的外部网站发送http post和获取请求,除非元素不包含name属性。下面是一个例子: 这是Java代码: public static String sendPostRequest(String url) { StringBuffer sb = null; try { String data = URLEncoder.encode("user", "UTF-8") + "="

所以我有了这段代码,我让它开始工作,现在它基本上允许我向几乎任何我想要的外部网站发送http post和获取请求,除非元素不包含name属性。下面是一个例子:

这是Java代码:

    public static String sendPostRequest(String url) {

    StringBuffer sb = null;

    try {

        String data = URLEncoder.encode("user", "UTF-8") + "="
                + URLEncoder.encode("myUserName", "UTF-8") + "&"
                + URLEncoder.encode("submit", "UTF-8") + "="
                + URLEncoder.encode("Submit", "UTF-8");


        URL requestUrl = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) requestUrl
                .openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("GET");

        OutputStreamWriter osw = new OutputStreamWriter(
                conn.getOutputStream());
        osw.write(data);
        osw.flush();

        BufferedReader br = new BufferedReader(new InputStreamReader(
                conn.getInputStream()));

        String in = "";
        sb = new StringBuffer();

        while ((in = br.readLine()) != null) {
            sb.append(in + "\n");
        }

        osw.close();
        br.close();
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return sb.toString();
}
这是我试图向其发送请求的表单(这是w3schools网站上的表单,即该网站):


用户名:

现在,由于Submit按钮没有name属性,我无法向它发送正确的HTTPGET/Post请求(我知道在这种情况下它是一个Get方法)。我应该用什么替换字符串数据(正确的键/值),以便它实际向该表单发送请求

我正在使用HttpClient生成http请求

HttpClient是开源apache项目。您可以获得广泛的代码。 HttpClient版本4.1是一套很好的Http api


您根本不向数据中添加
submit
部分。这只是为了让浏览器知道“提交”按钮触发操作。请注意新打开站点的URL的外观:-否
submit
parthere。因此,您的
数据
代码应该如下所示:

String data = URLEncoder.encode("user", "UTF-8") + "="
            + URLEncoder.encode("myUserName", "UTF-8"); // end here

//发出http get请求

HttpClient httpClientDefault1 = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://www.your.targer.url.com/page.html");
//设置标题(服务器理解某些浏览器抛出的请求)

httpPost.setHeader(“Referer”,resultur+resulturasp)

//设置参数

ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
nameValuePair.add(new BasicNameValuePair("key",""));
nameValuePair.add(new BasicNameValuePair("txtenroll","095020693015"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
//获取响应体

if(httpRespnse.getStatusLine().getStatusCode() != 200) {
    InputStream in =  httpRespnse.getEntity().getContent();
    byte b[] = new byte[1024] ;
    StringBuilder html = new StringBuilder("");
    while(in.read(b) != -1) {
        html.append((new String(b)).toString());
        b = new byte[1024];
    }
    System.out.println(html);
}

您还可以通过java代码获取标题、http参数、cookies、管理会话…:)

我有一个ClientHttpRequest类,它可以执行所有多部分、文件等操作,并提供可选的进度跟踪和取消。它已经存在大约10年了。使用起来非常简单。 现在也有了Scala版本


ehm不管我是在html\u forms\u action.asp网站还是在html\u forms.asp网站上使用它,它都不起作用。我在其他网站上也尝试过,除非我添加了submit按钮部分,否则它不起作用……这可能是因为调用
getOutputStream()
时,
GET
请求会自动替换为
POST
。这是一个合理的做法,因为这通常是作者的意思。要真正发出
GET
请求,请删除整个
OutputStreamWriter
部分,并将
数据添加到请求URL,就像它应该在有效的GET请求中一样:
URL requestUrl=新URL(URL+“?”+数据)。当然,您仍然没有添加
submit
部分,但这不是GET请求,而是直接获取某个URL的HTML源代码。我需要发送一个请求。我试图通过完全删除outputstream来实现这一点,但它仍然返回了错误的数据。这就是GET请求的真正含义——只是一个带有查询部分的URL(
?someData=value&otherData=value…
)。您也可以在浏览器地址栏中手动键入,然后按enter键。这与填写表单并按下
submit
按钮相同。把GET想象成POST,将所有数据从body移动到问号后面的URL中。这有点过于简单化了,但可以抓住这个想法。看看这里的完整代码:这与我试图做的有什么关系?我在问应该使用什么键/值向特定站点发送HTTP Post请求。。。不是关于某种API@user1007059:HttpClient将为您提供更好的可读性,您可以解决您的问题。。。我在我的项目中使用这个httpclien 4.1,并从任何URL获取数据。
ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
nameValuePair.add(new BasicNameValuePair("key",""));
nameValuePair.add(new BasicNameValuePair("txtenroll","095020693015"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
HttpResponse httpRespnse = httpClientDefault1.execute(httpPost);
if(httpRespnse.getStatusLine().getStatusCode() != 200) {
    InputStream in =  httpRespnse.getEntity().getContent();
    byte b[] = new byte[1024] ;
    StringBuilder html = new StringBuilder("");
    while(in.read(b) != -1) {
        html.append((new String(b)).toString());
        b = new byte[1024];
    }
    System.out.println(html);
}