簡體   English   中英

HttpURLConnection:獲取響應代碼400

[英]HttpURLConnection: Getting Response code 400

我正在使用HttpURLConnection從Java打服務器。

URL: http://clicks.pureleads.com/c/E1oTzCqNzcgtN8YfNs5wg25lxQ7X8*PEqjqks**E|l9gsgSg6ZyKU7FdR0WkUDvIZ2jgG9hhb1qfSlUJpaojsCMystWBEfw6EacQ4K)zd6CtREq0PA68Uj4rPqLEsBgQdK6pgV88epNw3LF7S3X4erNQmaF26N)47Eb8H9O58bmMrEwGTBJ*avyzVCGxuyuaAoqBkOBafBru3i44S*oP74Wu4toNaTW3onZrpV9qsIqsMb3kC9J2s4RDStFqTAzH0VPlO)ZI0SWtJAchAt76suO2vTJdu6WVynplETGZD4I6DvNiQ1CSnQtSE0G2ing((

對於URL編碼,請使用URLEncoder.encode(url, "UTF-8");

Encoded URL:
http://clicks.pureleads.com/c/E1oTzCqNzcgtN8YfNs5wg25lxQ7X8*PEqjqks**E%7Cl9gsgSg6ZyKU7FdR0WkUDvIZ2jgG9hhb1qfSlUJpaojsCMystWBEfw6EacQ4K)zd6CtREq0PA68Uj4rPqLEsBgQdK6pgV88epNw3LF7S3X4erNQmaF26N)47Eb8H9O58bmMrEwGTBJ*avyzVCGxuyuaAoqBkOBafBru3i44S*oP74Wu4toNaTW3onZrpV9qsIqsMb3kC9J2s4RDStFqTAzH0VPlO)ZI0SWtJAchAt76suO2vTJdu6WVynplETGZD4I6DvNiQ1CSnQtSE0G2ing((

當我嘗試在瀏覽器中打開上述URL時,響應代碼為OK(200) 服務器從Java返回response code 400

代碼段:

URI uri = new URI("http", "clicks.pureleads.com","/c/E1oTzCqNzcgtN8YfNs5wg25lxQ7X8*PEqjqks**E|l9gsgSg6ZyKU7FdR0WkUDvIZ2jgG9hhb1qfSlUJpaojsCMystWBEfw6EacQ4K)zd6CtREq0PA68Uj4rPqLEsBgQdK6pgV88epNw3LF7S3X4erNQmaF26N)47Eb8H9O58bmMrEwGTBJ*avyzVCGxuyuaAoqBkOBafBru3i44S*oP74Wu4toNaTW3onZrpV9qsIqsMb3kC9J2s4RDStFqTAzH0VPlO)ZI0SWtJAchAt76suO2vTJdu6WVynplETGZD4I6DvNiQ1CSnQtSE0G2ing((",null);
String url = uri.toString();
serverAddress = new URL(url);
connection = (HttpURLConnection) serverAddress.openConnection();
connection.setConnectTimeout(2000);
connection.setReadTimeout(2000);
connection.connect();

還嘗試了URLEncoder.encode(url,"UTF-8")對URL進行編碼。

提前致謝。

為我工作:

URI uri = new URI("http", "clicks.pureleads.com","/c/E1oTzCqNzcgtN8YfNs5wg25lxQ7X8*PEqjqks**E|l9gsgSg6ZyKU7FdR0WkUDvIZ2jgG9hhb1qfSlUJpaojsCMystWBEfw6EacQ4K)zd6CtREq0PA68Uj4rPqLEsBgQdK6pgV88epNw3LF7S3X4erNQmaF26N)47Eb8H9O58bmMrEwGTBJ*avyzVCGxuyuaAoqBkOBafBru3i44S*oP74Wu4toNaTW3onZrpV9qsIqsMb3kC9J2s4RDStFqTAzH0VPlO)ZI0SWtJAchAt76suO2vTJdu6WVynplETGZD4I6DvNiQ1CSnQtSE0G2ing((",null);
    String url = uri.toString();
    URL serverAddress = new URL(url);
    HttpURLConnection connection = (HttpURLConnection) serverAddress.openConnection();
    connection.setConnectTimeout(2000);
    connection.setReadTimeout(2000);
    connection.connect();

    InputStream inputStream = connection.getInputStream();

    String output = IOUtils.toString(inputStream);

字符串輸出包含以下內容:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <meta http-equiv="Refresh" content="1;URL='http://www.macys.com'">
<title></title>
</head>
<body onload='g();'>
<script type="text/javascript">
    var dest_url = "http://www.macys.com"
    var back_url = ""
    function g() {
        if (navigator.userAgent.indexOf("MSIE") != -1) {
           //The user has hit back button
           if (document.getElementById('final').value == 1) {
              document.getElementById('final').value = 0;
              //The ad opened in new window
              if (document.getElementById('hist').value == 0) {
                 self.location.replace(back_url);
              }
              //Ad opened in the same window
              else {
                 history.go(-1);
              }
           }
           else {
              //IE doesn't send referrer when used with replace
              var created_link = document.createElement("a");
              created_link.href = dest_url;
              document.body.appendChild(created_link);
              //set the back button variable and new window variable
              created_link.click(document.getElementById('final').value = 1,     document.getElementById('hist').value = history.length);
           }
        }
        else {
            if (document.images) {
                self.location.replace(dest_url);
            }
            else {
                self.location.href = dest_url;
            }
        }    
    }
</script>
<noscript><img src="noscript.gif"></noscript>
<form>
    <input type="hidden" name="u" value="0" id="final">
    <input type="hidden" name="h" value="-99" id="hist">
</form>
</body>
</html>

我從執行您的代碼中得到200的響應,也許您在代理或類似的支持下?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM