2017-08-31 17 views
7

tôi có mã này tạo ra một yêu cầu và đọc dữ liệu nhưng nó luôn luôn là trốngdữ liệu đọc từ url trả về giá trị rỗng

 static string uri = "http://yiimp.ccminer.org/api/wallet?address=DshDF3zmCX9PUhafTAzxyQidwgdfLYJkBrd"; 

    static void Main(string[] args) 
    { 

     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); 
     request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip; 

     HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 

     // Get the stream associated with the response. 
     Stream receiveStream = response.GetResponseStream(); 

     // Pipes the stream to a higher level stream reader with the required encoding format. 
     StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8); 

     Console.WriteLine("Response stream received."); 
     Console.WriteLine(readStream.ReadToEnd()); 
     response.Close(); 
     readStream.Close(); 

    } 

Khi tôi cố gắng để truy cập vào liên kết từ trình duyệt tôi nhận json này:

{"currency": "DCR", "unsold": 0.030825917365192, "balance": 0.02007306, "unpaid": 0.05089898, "paid24h": 0.05796425, "total": 0.10886323} 

Tôi đang thiếu gì?

+0

Bạn có gặp phải bất kỳ lỗi nào không? Mã trạng thái cho yêu cầu là gì? – Jerodev

+0

không có lỗi, chỉ cần một giá trị rỗng –

Trả lời

7

Khi bạn thực hiện yêu cầu từ trình duyệt, có rất nhiều tiêu đề được gửi đến dịch vụ web. Rõ ràng dịch vụ web này xác nhận UserAgent. Đây là một quyết định về một phần của việc triển khai dịch vụ web, họ có thể không muốn bạn truy cập theo chương trình.

var client = (HttpWebRequest)HttpWebRequest.Create(new Uri("http://yiimp.ccminer.org/api/wallet?address=DshDF3zmCX9PUhafTAzxyQidwgdfLYJkBrd")); 
client.AutomaticDecompression = DecompressionMethods.GZip; 
client.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063"; 
client.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate"; 
client.Host = "yiimp.ccminer.org"; 
client.KeepAlive = true; 

using (var s = client.GetResponse().GetResponseStream()) 
using (var sw = new StreamReader(s)) 
{ 

    var ss = sw.ReadToEnd(); 
    Console.WriteLine(ss); 
} 

Gửi tiêu đề dường như hoạt động.

+0

điều này dường như làm việc, thiếu tiêu đề là vấn đề, cảm ơn! –

Các vấn đề liên quan