iT邦幫忙

0

請教如何以Http post方式擷取有帳號密碼的json格式網頁

  • 分享至 

  • xImage

請教如何以Http post方式擷取有帳號密碼的json格式網頁
我已經把帳號密碼自動輸入並傳送了
學校有個API是可以看到九台校車的位置
https://ithelp.ithome.com.tw/upload/images/20181206/20113828bRoOV9nofn.png
這個json檔是需要帳號密碼用post表單的方式連線
不允許用get的方式存取
https://ithelp.ithome.com.tw/upload/images/20181206/20113828RgxGmFtJur.png
API我把它放在dropbox
想轉存javascript成陣列
在HTML方式呈現
可是無法抓取到http://120.101.66.86:8080/api/GetBusData
他都沒辦法連線存取
有甚麼方法可以讀取後去做切割

後續我想要做文字的看板和google maps的方式呈現。

謝謝大家

Homura iT邦高手 1 級 ‧ 2018-12-06 17:32:38 檢舉
那個API禁止跨網域要求,你沒辦法使用前端取得資料,所以下面allenlwh大使用C#來做是不錯的方法
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
allenlwh
iT邦高手 1 級 ‧ 2018-12-06 14:17:53
    public class ResponseSelectResult
    {
        public int totalSize { get; set; }
        public List<recordDetail> records { get; set; }
    }

    public class recordDetail
    {        
        public string Id { get; set; }
        public bool IsActive { get; set; }
        public string Name { get; set; }
        public string Username { get; set; }
    }
    public static List<recordDetail> Updaterecords = new List<recordDetail>();
    
    urlStr = "https://xxx/services/yyyy";
    HttpWebRequest request = WebRequest.Create(urlStr) as HttpWebRequest;
    request.Method = "POST";
    int totalSize=0;
    
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        string json;
                        using (var sr = new StreamReader(response.GetResponseStream()))
                        {
                            json = sr.ReadToEnd();
                        }

                        //Json反序列化                 
                        ResponseSelectResult objResponse = JsonConvert.DeserializeObject<ResponseSelectResult>(json);

                        totalSize = objResponse.totalSize;

                        for (int x = 0; x <= objResponse.records.Count() - 1; x++)
                        {
                            recordDetail rec = new recordDetail();                   
                            rec.Id = objResponse.records[x].Id;
                            rec.IsActive = objResponse.records[x].IsActive;
                            rec.Name = objResponse.records[x].Name;
                            rec.Username = objResponse.records[x].Username;
                            //將取得的資料存入陣列內
                            Updaterecords.Add(rec);                        
                            
                            rec = null;                    
                        }  
                        MessageBox.Show("處理筆數=" + totalSize);
                    }
                }     
    
allenlwh iT邦高手 1 級 ‧ 2018-12-07 12:09:09 檢舉
public class sendPost
{
    public string CoAccount { get; set; }
    public string Account { get; set; }
}
           urlStr="http://120.101.66.86:8080/api/GetBusData";
HttpWebRequest request = WebRequest.Create(urlStr) as HttpWebRequest;
request.ContentType = "application/json; charset=utf-8";

sendPost send = new sendPost();
send.Account = "871000";
send.CoAccount = "871000";

using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
    //Json序列化
    string json = JsonConvert.SerializeObject(send);
    streamWriter.Write(json);
    streamWriter.Flush();
}

我要發表回答

立即登入回答