C#Httpwebrequest手动重定向

时间:2018-05-03 11:42:45

标签: c# httpwebrequest httpwebresponse

有时一旦向链接发送请求,它会自动将我重定向到不需要的页面,所以为了防止这种情况,我尝试创建一个if语句进行重定向,但是我正在努力解决多个问题。

代码示例:

发送请求 -

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.util.Base64;import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

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


        try {
            URL url = new URL ("http://localhost:8080/job/SampleJob/build");
            String encoding = Base64.getEncoder().encodeToString(("admin:admin").getBytes());

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setRequestProperty  ("Authorization", "Basic " + encoding);
            InputStream content = (InputStream)connection.getInputStream();
            BufferedReader in   = new BufferedReader (new InputStreamReader (content));
            String line;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

获得回应 -

string requestUriString = "https://example.com/page1";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUriString);

功能

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            using (Stream stream2 = response.GetResponseStream())
            {
                using (StreamReader reader = new StreamReader(stream2, Encoding.UTF8))
                {
                    str6 = reader.ReadToEnd();
                }
            }
            return str6;
        }

将页面内容返回到要处理的原始应用程序。

但是我想为页面重定向实现一个if语句,我尝试过这样做 -

 return str6;

但它似乎不起作用,如

之后
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            using (Stream stream2 = response.GetResponseStream())
            {
                if ((int)response.StatusCode >= 300 && (int)response.StatusCode <= 399)
                {   
                    string newurl = "https://www.example.com/page2";

                    request = request = (HttpWebRequest)WebRequest.Create(newurl);

                }
                using (StreamReader reader = new StreamReader(stream2, Encoding.UTF8))
                {
                    str6 = reader.ReadToEnd();
                }
            }
            return str6;
        }

我需要它继续进行

的初始过程
if ((int)response.StatusCode >= 300 && (int)response.StatusCode <= 399)

0 个答案:

没有答案